Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Static Variables and Methods:-

you can use a static method or variable without having any instances of that class at all.
You need only have the class available to be able to invoke a static method or access a static variable. static
variables, too, can be accessed without having an instance of a class. But if there are instances, a static variable
of a class will be shared by all instances of that class, there is only one copy.
class Count{
static int count = 0; // Declare and initialize
// static variable
public Count() {
count += 1; // Modify the value in the constructor
}
public static void main (String [] args) {
new Count();
new Count();
new Count();
System.out.println("count is now " + count );
}
}
Now imagine what would happen if count were an instance variable (in
other words, nonstatic):
Complier will slap you:- Count.java:11: nonstatic variable count cannot be referenced from a static context
System.out.println("count is now " + count );
Note:-static = class, nonstatic = instance.
Accessing Static Methods and Variables:-
class Frog {
static int frogCount = 0; // Declare and initialize
// static variable
public Frog() {
frogCount += 1; // Modify the value in the constructor
}
}
class TestFrog {
public static void main (String [] args) {
new Frog();
new Frog();
new Frog();
System.out.print("frogCount:"+Frog.frogCount); //Access
// static variable
}
}
But just to make it really confusing, the Java language also allows you to use an
object reference variable to access a static member:
Frog f = new Frog();
int frogs = f.frogCount; // Access static variable
// FrogCount using f
Note:- Remember that static methods can't be overridden! This doesn't mean they
can't be redefined in a subclass, but redefining and overriding aren't the same thing.
class Animal {
static void voice() {
System.out.print(“Different of different animals. ");
}
}
class Dog extends Animal {
static void voice() { // it's a redefinition,
// not an override
System.out.print(“bark");
}
public static void main(String [] args) {
Animal obj=new Dog();
Obj.voice();
}
}
Type Casting
Type casting is a technique by which can explicitly change the type of a value.
1. Primitive Type Casting 2. Reference Type Casting.
Primitive Type Casting:-
It applied on the primitive data type.
A widening conversion is a conversion from a smaller numeric type to a larger numeric type.
 byte b = 8;  int i = b; // Implicit cast
A narrowing conversion is a conversion from a larger numeric type to a possible smaller numeric type. It require the cast operator.
int i = 0; byte b =i; // error possible loss of precision int i = 256; byte b =(byte)i;
long l = 10000; short s = (short)l; long l = 65; char s = (char)l; //fine it is called Explicit  cast
long l = 65; boolean s = (boolean)l; // error inconvertible type
Assignment conversion happens when you assign a value to a variable of a different type . int i=10; double d=i;d=10.0
Rules:-
1. If either is of type double, the other is converted to double.
2. Otherwise, if either is a float, the other is converted to float.
3. Otherwise, if either is of type long, the other is converted to long.
4. Otherwise, both operands are converted to int.
A non-boolean primitive data type can be converted to another non-boolean type if the conversion is a widening conversion.
A non-boolean primitive data type cannot be converted to another non-boolean type 
if the conversion would be a narrowing conversion.
A boolean cannot be converted to any other type.

int i=1; double d = 2; int j = i+d; //Error solution is:- j=(int)(i+d) ;


The String + operator results in the concatenation of two String objects. String a = "a"; String b = "b"; String c = a+b;
When one of the operands in a + operation is a String, then the other operand is converted to a String.
System.out.println(20+"10"+5);  20105 System.out.println(20+10+”5”);  305
Casting and the += Operator:- byte b = 0; b = 128;//error b+=128;//fine
Reference Variable Casting:- Upcasting and Downcasting.
Upcasting: When we share Perent class into child class from up to down.
class Animal { }
class Dog { }
class DogTest {
public static void main(String [] args) {
Animal animal = new Animal();
Dog d = (Dog) animal; // compile time error- Type mismatch
}
}

class Animal { }
class Dog extends Animal { }
class DogTest {
public static void main(String [] args) {
Animal animal = new Animal();
Dog d = (Dog) animal; // compiles but fails later
}
} Runtime Error:-java.lang.ClassCastException
To avoid this error use:-
if(animal instanceof Dog) {
Dog d = (Dog) animal; // casting the ref. var.
d.methodOfDog();
}
class Animal { }
class Dog extends Animal { }
class DogTest {
public static void main(String [] args) {
Dog d = new Dog();
Animal a1 = d; // upcast ok with no explicit cast
Animal a2 = (Animal) d; // upcast ok with an explicit cast
}
}
There are three places in java where you can perform operations: -
•Method
•Constructor
•Block
There are two types of initialization blocks:-
•instance initialization(init) blocks.
•static initialization blocks.
Instance Initializer block is used to initialize the instance data member. It run each time when object of the
class is created.
class Bike{  
    int speed;  

    Bike(){
System.out.println(“Inside constructor");
System.out.println("speed is "+speed);
}  
 {
System.out.println(“Inside init block");
speed=100;
}  
       
    public static void main(String args[]){  
    Bike b1=new Bike();  
    Bike b2=new Bike();  
    }      
}  
Static Initialization
This block runs once ,when class is first loaded.
public class Demo {
static{
System.out.println("init block1");
}
static{
System.out.println("init block2");
}
public static void main(String[] args) {
}
}
Generalization:-
Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure
and behaviour are used from the specializtion to the generalized class.  Ex:- Animal--- Dog, Animal-
Cat

Association:-
Association is a relationship between two objects.
Example: A Student and a Faculty are having an association.
1. Association in Java
We call association those relationships whose objects have an independent
lifecycle and where there is no ownership between the objects.

Let’s take an example of a teacher and student. Multiple students can associate with a
single teacher, and a single student can associate with multiple teachers, but both
have their own lifecycles (both can be create and delete independently); so when a
teacher leaves the school, we don’t need to delete any students, and when a
student leaves the school, we don’t need to delete any teachers.
Aggregation:- Aggregation is a special case of association. A directional association
between objects. When an object ‘has-a’ another object, then you have got an
aggregation between them. Aggregation is also called a “Has-a” relationship.
class Author{
private String name;
private String place;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public Author(String name, String place) {
super();
this.name = name;
this.place = place;
}}
Aggregation Example:-
class Book{
private Author auther; // create the public getter and setter methods for it to
keep existence of Author out side the Book
Book(String name,Author auth){
System.out.println("Book Name="+name);
System.out.println("Author Name="+auth.getName());
System.out.println("Author Place="+auth.getPlace());
}
public static void main(String[] arg){
Author auth=new Author("Kethy", "US");
Book bk=new Book("Java",auth);
}
}
Composition:-
Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called
composition. When an object contains the other object, if the contained object cannot exist without the
existence of container object, then it is called composition.
Example: A class contains students. A student cannot exist without a class. There exists composition between
class and students.
Difference between aggregation and composition
Composition is more restrictive. When there is a composition between two objects, the composed object
cannot exist without the other object. This restriction is not there in aggregation. 

Example: A Library contains students and books. Relationship between library and student is
aggregation. Relationship between library and book is composition. A student can exist
without a library and therefore it is aggregation. A book cannot exist without a library and
therefore its a composition.
class Car {
private final Engine engine; // There will be no public getter methods for engine.

Car(String carName) {
engine = new Engine(carName);
}

void move() {
engine.work();
}
}
Thank You….

You might also like