Beruflich Dokumente
Kultur Dokumente
Every time an object is created using new() keyword, at least one constructor is called. It
is called a default constructor.
class Bike1{
Bike1(){System.out.println("Bike is created");}
class Student4{
int id;
String name;
, a constructor is just like a method but without return type. It can also be overloaded
like Java methods.
Constructor overloading is a technique of having more than one constructor with different
parameter lists. They are arranged in a way that each constructor performs a different task.
They are differentiated by the compiler by the number of parameters in the list and their
types.
class Student5{
int id;
String name;
int age;
Student5(int i,String n){
id = i;
name = n;
}
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}
Static Keyword:
The static keyword is used for memory management mainly.
class Student8{
int rollno;
String name;
static String college ="ITS";
s1.display();
s2.display();
}
}
this Keyword:
this is a reference variable that refers to the current object.
parameters (formal arguments) and instance variables are same. So, we are using
this keyword to distinguish local variable and instance variable.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
You may invoke the method of the current class by using the this keyword. If you
don't use the this keyword, compiler automatically adds this keyword while invoking
the method.
this() constructor call can be used to invoke the current class constructor. It is
used to reuse the constructor. In other words, it is used for constructor chaining.
class A{ class A{
A(){System.out.println("hello a");} A(){
A(int x){ this(5);
this(); System.out.println("hello a");
System.out.println(x); }
} A(int x){
} System.out.println(x);
class TestThis5{ }
public static void main(String args[]){ }
A a=new A(10); class TestThis6{
}} public static void main(String args[]){
A a=new A();
}}
Inheritance:
Inheritance is a mechanism in which one object acquires all the properties and
behaviours of parent object.
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
class Employee{
float salary=40000;
}
Types of inheritance:
Single Inheritance:
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Multilevel Inheritance:
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Hierarchical Inheritance:
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
class Operation{
int square(int n){
return n*n;
}
}
class Circle{
Operation op;//aggregation
double pi=3.14;
Method Overloading:
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
method overloading is not possible by changing the return type of the method only
because of ambiguity.
Method Overriding:
If subclass (child class) has the same method as declared in the parent class, it
is known as method overriding .
class Bank{
int getRateOfInterest(){return 0;}
}
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
super keyword:
The super keyword is a reference variable which is used to refer immediate parent
class object.
super is used to refer super can be used to invoke super is used to invoke
immediate parent class parent class method parent class constructor
instance variable:
class Animal{ class Animal{
class Animal{ void Animal(){System.out.printl
String color="white"; eat(){System.out.println("ea n("animal is created");}
} ting...");} }
class Dog extends } class Dog extends Animal{
Animal{ class Dog extends Animal{ Dog(){
String color="black"; void super();
void printColor(){ eat(){System.out.println("ea System.out.println("dog is
System.out.println(color ting bread...");} created");
);//prints color of Dog void }
class bark(){System.out.println("b }
System.out.println(super arking...");} class TestSuper3{
.color);//prints color void work(){ public static void
of Animal class super.eat(); main(String args[]){
} bark(); Dog d=new Dog();
} } }}
class TestSuper1{ }
public static void class TestSuper2{
main(String args[]){ public static void
Dog d=new Dog(); main(String args[]){
d.printColor(); Dog d=new Dog();
}} d.work();
}}
Final Keyword:
The final keyword is used to restrict the user. The java final keyword can be used
in many context. Final can be:
variable
method
class
If you make any variable as final, you cannot change the value of final variable(It
will be constant).
If you make any method as final, you cannot override it.
If you make any class as final, you cannot extend it.
Polymorphism:
There are two types of polymorphism : compile time polymorphism and runtime
polymorphism. We can perform polymorphism by method overloading and method
overriding.
Abstract :
Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
A class that is declared with abstract keyword, is known as abstract class . It can
have abstract and non-abstract methods (method with body).
it shows only important things to the user and hides the internal details for
example sending sms, you just type the text and send the message. You don't know
the internal processing about the message delivery.
class TestBank{
public static void main(String args[]){
Bank b=new SBI();//if object is PNB, method of PNB will be invoked
int interest=b.getRateOfInterest();
System.out.println("Rate of Interest is: "+interest+" %");
}}
Interface:
An interface is a blueprint of a class. It has static constants and abstract
methods.
There are mainly three reasons to use interface. They are given below.
It is used to achieve abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.
interface Printable{
void print();
}
interface Showable{
void show();
}
class Test{
public static void main(String[] args){
Student s=new Student();
s.setName("vijay");
System.out.println(s.getName());
}
}