java unit 3.java
java unit 3.java
Syntax:-
Class A//super class
{
}
Class B extends A//sub class
{ }
Types of inheritance
1.single/simple inheritance :-it contain only one
superclass and one subclass.but subclass n is cannot
connect directly with superclass it connected firstly
previous subclass.
2.Multilevel inheritance :-it contain more than one
subclass but only one super class.
3.Multiple inheritance:- java does not support multiple
inheritance because it has two superclasses and only one
subclasses
4.heirarchical inheritance:- it contain more than one
subclass but only one super class. But in this type subclass
is directly connect with super class
Single inheritance
Syntax:-
Class A//super class
{
}
Class B extends A//sub class
{ }
Example program…..
class student //super class
{
int roll,marks;
String name;
void input()
{
System.out.println("enter roll no & name & marks : ");
}
}
class nikita extends student //sub class
{
void disp()
{
roll=15; name="nikita"; marks=90;
System.out.println(roll+" "+name+" "+marks);
}
public static void main(String[] args)
{
nikita n=new nikita();
n.input(); n.disp();
}
}
Output:-
Multi-level inheritance…
Syntax :-
Class super
{
}
Class sub1 extends super
{
}
Class sub2 extends sub1
{
}
Example program….
class A //super class
{
int a,b,c;
void add() //method
{
a=10;b=20;
c=a+b;
System.out.println("addition of two numbers is "+c);
}
void sub()
{
a=100;b=200;
c=a-b;
System.out.println("substraction of two numbers is "+c);
}
}
class B extends A //sub class 1
{
void mul()
{
a=10;b=20;
c=a*b;
System.out.println("multiplication of two numbers is "+c);
}
void div()
{
a=100;b=2;
c=a/b;
System.out.println("division of two numbers is "+c);
}
}
class C extends B //sub class 2
{
void rem()
{
a=10;b=2;
c=a%b;
System.out.println("reminder of two numbers is "+c);
}
}
class Test1 //main class…
{
public static void main(String[] args)
{
C r=new C(); //creating object
r.add();
r.sub(); //method calling
r.mul();
r.div();
r.rem();
}
}
Output:-
What is interface?
Interface is just like a class,which contains only abstract
method .
To achieve interface in java it provides a keyword called
implements…..
Output:-
Hierarchical inheritance….
Syntax :_
Class A
{
}
Class B extends A
{
}
Class C extends A
{
}
Example program…..
class A
{
void show()
{
System.out.println("hello guys");
}
}
class B extends A
{
void disp()
{
System.out.println("Welcome guys");
}
}
class C extends A
{
void play()
{
System.out.println("good night");
}
}
class Demo1
{
public static void main(String[] args)
{
B a=new B();
C a1=new C();
A a2=new A();
a.disp();
a1.play();
a2.show();
}
}
Output:-
Example:-
Method Overloading:-
Whenever class contain more than one
method with same name and different types of parameters is
called as method overloading…
Syntax:-
Return_type method_name(para1);
Return_type method_name(para1,para n);
Void display(int a);
Example:-
Output:-
Runtime polymorphism
A polymorphism which exists at the time of execution of the
program is called as runtime polymorphism..
Example..
Method overriding
Whenever we writing a method name in
super class and sub classes in such a way that the method name
and parameter must be same is called as method overriding..
We cannot perform method overriding without inheritance.
Syntax:-
Class A
{
void show()
{
}
}
Class B extends A
{
Void show()
{
}
}
Output:-
Finally
Finally is a block which is used in in try catch block .
This block is must be execute in try catch block if the
exception is handled or not this block must be executed.
Syntax:-
Try
{
}
Catch{
//handeling code
}
Finally
{
Clean up code;
}
Finalize method
Finalize{}
Finalize is a method
It is used to deallocate the resources which is allocated by
unused object
Super keyword
Super keyword refers to the objects of super class,it is used
when we want to call the super class variable ,method &
constructor through sub class object.
Whenever the super class & sub class variable and method
name both are same than it can be used only
To avoid the confusion between super class and sub
classes variables & methods thatc have same name we
should use super keyword…
Using super keyword we can access super class
variable ,method and constructor.
–
Output:-