0% found this document useful (0 votes)
0 views

java unit 3.java

The document explains the concept of inheritance in Java, detailing various types such as single, multilevel, multiple, and hierarchical inheritance. It also covers method overriding, interfaces, and polymorphism, including compile-time and runtime polymorphism. Additionally, it discusses the use of the final keyword, the finally block, and the super keyword in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

java unit 3.java

The document explains the concept of inheritance in Java, detailing various types such as single, multilevel, multiple, and hierarchical inheritance. It also covers method overriding, interfaces, and polymorphism, including compile-time and runtime polymorphism. Additionally, it discusses the use of the final keyword, the finally block, and the super keyword in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Unit 3

Inheritance:- when we construct a new class from


existing class in such a way that the nwew class
access all the features & properties of existing
class.
 In java extends keyword is used to perform
inheritance.
 It provides code reusability
 We cannot access private members of class through
inheritance.
 A sub class contains all the features of super class so,
we should create the object of sub class
 Method overriding oonlyb possible through method
overloading.

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:-

Multilevel inheritance :- whenever a subclass wants to


inherit the property of two or more super class that hava
same method,java compiler cant decide which class
method it should inherit.
Then their might be a chance of memory duplication that
is ,java does not support…………..multiple inheritance.
Synatax:-
Class A
{
Void m1()
{
}}
Class B
{
Void m1()
{
}
Class C extends A,B
{
C is in confusion they doesn’t decide which class they will
have to extend
}

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…..

 Multiple inheritance using interface


We can achieve multiple inheritance through interfaces
because interface contains only abstract method ,which
implementation is provided by the sub class.
Class C extends A,B //invalid
Class C implements A,B // valid…
Creating interface
Syntax:-
Interface interface name
Example:-
interface A
{
void show();
}
interface B
{
void show();
}
class multiple implements A,B
{
public void show()
{
System.out.println("Interface A & B");
}
public static void main(String[] args)
{
multiple m=new multiple();
m.show();
}
}

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:-

 Compile time polymorphism


A polymorphism which is exist at the time of compilation is called
as compile time polymorphism or early binding polymorphism or
static polymorphism.

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:-

 Dynamic method dispatch


 Dynamic method is also called as a runtime
polymorphism
 Dynamic method dispatch is a mechanism that decides
which overridden method will be called at runtime.

Final ,finally, finalize


Final is a keyword which we can apply with
variable ,method and class.
Example…
Final int a=10;
In the case of final method we can not override the
method …
In the case of final class we cannot create the extends any
class means we cannot create subclass or child class this
must be only final class..

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:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy