0% found this document useful (0 votes)
12 views50 pages

C @unit-Iv

Uploaded by

pushpascholar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views50 pages

C @unit-Iv

Uploaded by

pushpascholar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Module-iv

INHERITANCE

TYPES OF INHERITANCE

POLYMORPHISM

TYPES OF POLYMORPHISM

INTERFACE

ABSTRACT CLASS

INNER CLASS

REFLECTION
Inheritance in Java
 Process of deriving new class from the existing class
Base class
Class Bike
 The existing class is known as- Gear
break
Base Class / Super class / Parent Class

Class Car
 The new class is known as- Gear
Derived class/ Sub Class / Child class Break
fuel
Derived class
 The derived classes inherit all the properties of the base
classes and its own properties.
Uses of inheritance

 Method Overriding (used for Runtime Polymorphism)

 Code Re-usability

Syntax of Inheritance

class Subclass_Name extends Superclass_Name


{
//methods and fields
}
Keywords in inheritance

 extends-Indicates that a class is inherited from another class

 implements-Instead of extends keyword

 super- Used to invoke the constructor of base to sub classes .

SYNTAX : super(argument list);


Super keyword

 In inheritance don’t create object for base class because of the objects
created only for sub classes.

 Sub classes can only be accessed base class objects(attributes and


methods).

 Sub classes can’t be accessed by base class constructor .

 The super Keyword is used with following Conditions

 super may only be used within a subclass constructor.

 The call to super must appear as first statement.


TYPES OF INHERITANCE
Types of inheritance
in java

 On the basis of class, there can be three types of inheritance.

 Single Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance

 In java programming, multiple and hybrid inheritance is supported


through interface only.
Single inheritance
 A derived class to inherit the properties and
behavior from a single parent class.

 Here A is a parent class of B and B would be


a child class of A.
A parent

B child
sYNTAX
class A
{
public void print()
{
System.out.println("Base class method");
}
}
class B extends A
{
OUTPUT
public void display()
{ Base class method
System.out.println("Child class method");
Child class method
}
public static void main(String args[])
{
B obj = new B();
obj.print(); //calling super class method
obj.display(); //calling local method
}}
class add
{ public static void main(String args[])
int a,b,c; {
void add(int x,int y) mul m=new mul();
{ m.add(10,20);
a=x; m.mul(10,20);
b=y; }
c=a+b; }
System.out.println(c);
}
}
class mul extends add
{
void mul(int s,int r)
{ OUTPUT
c=s*r;
System.out.println(c); 30
}
200
Multilevel Inheritance
 A class derived from other derived class or have multiple
levels of single inheritances.

 Here C is subclass class of B and B is a subclass class of A.

A Base class

B Subclass/intermediate class

C subclass
sYNTAX
class A
public static void main(String args[])
{
{
public void print()
C obj = new C();
{
obj.print();
System.out.println("Base class method");
obj.display();
}
obj.result();
}
}}
class B extends A
{
public void display()
{ OUTPUT
System.out.println(“Middle class method");
} Base class method
}
class C extends B Middle class method
{
public void result() Subclass method
{
System.out.println("subclass method");
}
class add void print1() add a=new add(10,20);
{ { a.print();
int a,b,c; System.out.println(c); mul m=new
add(int x,int y) } mul(10,20);
{ } m.print1();
a=x; class div extends mul div d=new div(10,20);
b=y; { d.print2();
c=a+b; div(int e,int w) }
} { }
void print() super(e,w);
{ c=e/w;
System.out.println(c); }
} void print2()
} {
class mul extends add System.out.println(c);
{ }
mul(int s,int r)
{
public static void main(String args[])
super(s,r);
c=s*r; {
HIERARCHICAL INHERITANCE

 A base class has more than one sub classes or in other


words, more than one child classes have the same
parent class
 In below example class B,C and D inherits the same
class A.
 A is base class of B,C & D.

B C D
syntax
class Rect {
{ Triangle(double d, double h)
double d1, d2,area; {
Rect(double x, double y) super(d,h);
{ area=(d*h)/2; OUTPUT
d1 = x; d2 = y; area=d1*d2; } 40.0
} void print2() 6000.0
void print() { 6.0
{ System.out.println(area);
System.out.println(area); }}
} class Result
} {
class Box extends Rect public static void main(String[] args)
{ {
double width; Rect r = new Rect(5,8);
Box(double l, double b,double w) r.print();
{ Box b=new Box(10,20,30);
super(l,b); b.print1();
width=w; Triangle t = new Triangle(4,3);
area=l*b*w; t.print2();
} }}
void print1()
{
System.out.println(area);}}
Polymorphism

Students

Customer

Passenger
Uses

The word polymorphism can be broken down


into Poly and morphs, as “Poly” means many and “Morphs”
means forms.

Define polymorphism as the ability of a message to be


displayed in more than one form.
Types of polymorphism
Compile time
polymorphism

It is also known as static polymorphism.


1) Method overloading(function overloading):
When there are multiple functions with same name but
different parameters then these functions are said to be
overloaded.
Functions can be overloaded by
 Change in type of arguments.
 Change in number of arguments.
Change in Type of arguments:
import java.io.*;
class one
{
static int add(int a, int b)
{
return a+b;
}
static double add(double a,double b) // diff types of argu
{
return a+b;
} OUTPUT
public static void main(String args[])
{ 5
5.5
System.out.println(one.add(2,3));
System.out.println(one.add(2.2,3.3));
}
}
Change in number of arguments
import java.io.*;
class one
{
static int add(int a,int b)
{
return a+b;
}
static int add(int a,int b,int c) // increasing arguments
{
return a+b+c;
} OUTPUT
public static void main(String args[]) 5
{ 8
System.out.println(one.add(2,3));
System.out.println(one.add(2,3,3));
}
Operator Overloading
import java.io.*;
class one
{
void add(int a,int b)
{
System.out.println(a+b);
}
void add(String a,String b)
{
System.out.println(a+b);
} OUTPUT
public static void main(String args[])
Thank you
{ 5
one t1=new one();
t1.add("Thank ","you");
t1.add(2,3);
}
}
Runtime Polymorphism

It is also known as dynamic polymorphism.

Method Overriding:
It occurs when a derived class has a definition for one of the
method of the base class is known as method overriding.
Class Student1
{
Void marks()
{
}
Class Student2 extends student1
{
Void marks()
{
}
class Bank public static void main(String args[])
{ {
float getROI() Bank b;
{ b=new SBI();
return 0; S .o.p("SBI Rate of Interest: "+b.getROI());
}
} b=new ICICI();
class SBI extends Bank S.o.p("ICICI Rate of Interest: "+b.getROI());
{ }
float getROI() }
{
return 8.4f; OUTPUT
}
} SBI Rate of Interest :8.4
class ICICI extends Bank
{ ICICI Rate of Interest:7.3
float getROI()
{
return 7.3f;
}
Abstract class in Java

Abstraction in Java
 Abstraction is a process of hiding the implementation
details and showing only functionality to the user

Ways to achieve Abstraction

 There are two ways to achieve abstraction in java

Abstract class

Interface
Abstract keywords

Abstract class

 A class which is declared with the abstract keyword is known


as an abstract class in Java.

 It can have abstract and non-abstract methods

Syntax
abstract class class_name
{

}
Abstract Method

 A method which is declared as abstract and does


not have implementation is known as an abstract
method.

Syntax:
abstract void print_Status();
Example of Abstract class &abstract method

abstract class Bike


{
abstract void run();
}
class Honda4 extends Bike
{
void run()
{
System.out.println("Running Safely");
}
OUTPUT
public static void main(String args[])
{ Running Safely
Bike obj = new Honda4();
obj.run();
}
Abstract class with normal method
abstract class test1
{
public void add()
{
System.out.println("This is abstract class method");
}
}
class test2 extends test1
{ OUTPUT

} This is abstract class method


class test
{
public static void main(String args[]) // main function
{
test2 t2=new test2(); // t2->object
t2.add();
}
}
interface in Java
 The interface in Java is a blue print of class and mechanism
to achieve abstraction.

 There can be only abstract methods in the Java interface,


not method body.

 It is used to achieve abstraction and multiple inheritance


in Java.
Uses of interface

 There are mainly three reasons to use interface.

 It is used to achieve abstraction.

 By interface, we can support the functionality of


multiple inheritance.

 It can be used to achieve classes are independent of


each other.
Syntax:
interface interface_name
{
// declare fields and methods that abstract
}
Implementing interface

class class_name implements interface1,interface2,….


{

}
interface Print
{
void print();
}
class Display implements Print
{
public void print()
{
System.out.println(“This is interface classes");
}
public static void main(String args[])
{
Display obj = new Display(); OUTPUT
obj.print();
} This is interface classes
}
INTERFCE USING MULTIPLE INHERITANCE

interface animal class test


{ {
public void dog(); public static void main(String args[])
} {
interface bird test1 t1=new test1();
{ t1.dog();
public void cat(); t1.dove();
} }
class test1 implements animal, bird }
{
public void dog()
{ OUTPUT
System.out.println("wow.. wow..");
} wow.. wow..
public void cat() meow...
{
System.out.println(“meow...");
}
}
Inner class in Java

 Inner class means one class which is a member of another class.

 There are basically four types of inner classes in java.

1) Nested Inner class


2) Method Local inner classes
3) Anonymous inner classes
4) Static nested classes
Nested Inner class

 Nested Inner class can access any private instance variable of


outer class.
class outer // outer class
{
class inner // inner class
{
public void show() // inner class method
{
System.out.println("WELCOME");
}
}
public static void main(String args[]) OUTPUT
{
outer.inner in=new outer().new inner(); WELCOME
in.show();
}
}
Method Local inner classes

 Inner class can be declared within a method of an outer class.

class outer // outer class class test


{ {
public void add() // outer class method public static void main(String args[])
{ {
System.out.println("This is outer class"); outer y=new outer();
class inner // inner class y.add();
{ }}
public void sub() // inner class method
{
System.out.println("This is inner class"); OUTPUT
} This is outer class
}
inner x=new inner(); This is inner class
x.sub();
}
}
STATIC NESTED CLASSES
 We can not declare top-level class with a static modifier, but can declare
nested classes as static.
class test
{
static class one // static nested class
{
String b=“Static Nested classes";
}
public static void main(String args[])
{ OUTPUT
test.one t1=new test.one();
Static Nested classes
System.out.println(t1.b);
}
}
Anonymous inner classes

 A class can contain another class known as nested class.It's


possible to create a nested class without giving any name.
 A nested class that doesn't have any name is known as an
anonymous class.
 An anonymous class must be defined inside another class it is
also known as an anonymous inner class.
class outerClass
{
object1 = new Type(parameterList) // defining anonymous class
{
// body of the anonymous class
};
}
interface hello
{
void show();
}
class test
{
static hello h=new hello()
{
public void show()
{
System.out.println("Anonymous class");
}
};
OUTPUT
public static void main(String args[])
{ Anonymous class
h.show();
}
Reflection in Java
 Reflection is an API which is used to examine or modify
the behavior of methods, classes ,interfaces at runtime.
get the instance of Class
There are 3 ways to get the instance of Class class

1. forName() method of Class class


2. getClass() method of Object class
3. the .class syntax
forName() method of Class class
class Simple
{
}
class Test
{
OUTPUT
public static void main(String args[])
{ Simple
Class c=Class.forName("Simple");
System.out.println(c.getName());
}
}
getClass() method of Object class
class Simple
{
}
class test OUTPUT
{
void printName(Object obj) Simple
{
Class c=obj.getClass();
System.out.println(c.getName());
}
public static void main(String args[])
{
Simple s=new Simple();
test t=new test();
t.printName(s);
}
}
.class syntax
import java.io.*;
class test
{
public static void main(String args[])
{
OUTPUT
Class c = test.class;
System.out.println(c.getName()); test

}
}

You might also like