Chapter 3-Final
Chapter 3-Final
The process of obtaining the data members and methods from one class to
another class is known as inheritance. It is one of the fundamental features
of object-oriented programming.
Important points
In the inheritance the class which is give data members and methods
is known as base or super or parent class.
The class which is taking the data members and methods is known
as sub or derived or child class.
The data members and methods of a class are known as features.
The concept of inheritance is also known as re-usability or
extendable classes or sub classing or derivation.
Syntax of Inheritance
Syntax
class ClassName_1
{
Variable_1 declaration;
Method_1 declaration;
}
class ClassName-2 extends ClasssName-1 (Logically Linked)
{
Variable_2 declaration; Variable_1 declaration;
Method_2 declaration; Method_1 declaration;
}
Type of Inheritance
Based on number of ways inheriting the feature of base class into derived
class we have five types of inheritance; they are:
Single inheritance
Multiple inheritance(Not with the help of classes)
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance
Single inheritance
In single inheritance there exists single base class and single derived class.
class Faculty
{
float salary=30000;
}
class Science extends Faculty
{
float bonous=2000;
void totalSal()
{
System.out.println("Actual salary credited into account: "+(salary+bonous));
}
}
class SingleEx
{
public static void main(String args[])
{
Science obj=new Science();
System.out.println("Salary is:"+obj.salary);
System.out.println("Bonous is:"+obj.bonous);
obj.totalSal();
}
}
class B extends A
class C extends B
class D extends C
Hence all the above three inheritance types are supported by both classes and
interfaces.
When more than one classes inherit a same class then this is called hierarchical
inheritance. For example class B, C and D extends a same class A.Class
Lets
A see the
diagram representation of this:
{
class B extends A{
class C extends A
As you can see in the above diagram that when a class has more than one child
classes (sub classes) or in other words more than one child classes have the
same parent class then this type of inheritance is known as hierarchical
inheritance.
class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class JavaExample
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
//All classes can access the method of class A
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
Hybrid inheritance
OVERLOADING METHODS
In Java it is possible to define two or more methods within the same class that
share the same name, as long as their parameter declarations are different. When
this is the case, the methods are said to be overloaded, and the process is
referred to as method overloading.
OVERLOADING CONSTRUCTORS
In addition to overloading normal methods, you can also overload constructor
methods. In fact, for most real-world classes that you create, overloaded
constructors will be the norm, not the exception.
Syntax :
class A
A(){ }
Class B
{A a=new A();
A a1=new A(56,89.67);
A a2=new A(90,89);}
class Box
{
double width;
double height;
double depth; // This is the constructor for Box.
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box()
{
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box }
// constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
class OverloadCons
{
public static void main(String args[])
{
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol; // get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}
Method Overriding in Java
Whenever same method name is existing in both base class and derived class
with same types of parameters or same order of parameters is known
as method Overriding. Here we will discuss about Overriding in Java.
Note: Without Inheritance method overriding is not possible.
In this example, we have defined the walk method in the subclass as defined
in the parent class but it has some specific implementation. The name and
parameter of the method is same and there is IS-A relationship between the
classes, so there is method overriding.
Example
class Walking
{
void walk()
{
System.out.println("Man walking fastly");
}
}
class Man extends Walking
{
void walk()
{
System.out.println("Man walking slowly");
}
}
class OverridingDemo
{
public static void main(String args[])
{
Man obj = new Man();
obj.walk();
}
}
Note: Whenever we are calling overridden method using derived class object
reference the highest priority is given to current class (derived class). We can
see in the above example high priority is derived class.
Note: super. (super dot) can be used to call base class overridden method in
the derived class.
It makes a method final, meaning that sub classes can not override this
method. The compiler checks and gives an error if you try to override the
method.
Example
public class A
{
public void fun1()
{
.......
}
public final void fun2()
{
.......
}
}
class B extends A
{
public void fun1()
{
.......
}
public void fun2()
{
// it gives an error because we can not override final method
}
}
class Employee
{
final void disp()
{
System.out.println("Hello Good Morning");
}
}
class Developer extends Employee
{
void disp()
{
System.out.println("How are you ?");
}
}
class FinalDemo
{
public static void main(String args[])
{
Developer obj=new Developer();
obj.disp();
}
}
Output
It gives an error
It makes a class final, meaning that the class can not be inheriting by other
classes. When we want to restrict inheritance then make class as a final.
Example
We know that every Java program must start with a concept of class that is
without the class concept there is no Java program perfect
In Java programming we have two types of classes they are
Concrete class
Abstract class
Example
class Helloworld
{
void display()
{
System.out.println("Good Morning........");
}
}
Create an object
Every concrete class has specific features and these classes are used for
specific requirement, but not for common requirement.
Abstract method
Syntax
class A
void display()
System.out.println(“””);
Syntax }
Example
abstract class A
{
.....
}
If any class have any abstract method then that class become an abstract
class.
Example
class Vehicle
{
abstract void bike();
}
Example
Example
Super keyword in java is a reference variable that is used to refer parent class
object. Super is an implicit keyword create by JVM and supply each and
every java program for performing important role in three places.
Super keyword At Variable Level
Super keyword At Method Level
Super keyword At Constructor Level
super.baseclass datamember name
Example
class Employee
{
float salary=10000;
}
class HR extends Employee
{
float salary=20000;
void display()
{
System.out.println("Salary: "+super.salary);//print base class salary
}
}
class SuperEx
{
public static void main(String[] args)
{
HR obj=new HR();
obj.display();
}
}
The super keyword can also be used to invoke or call parent class method. It
should be use in case of method overriding. In other word super
keyword use when base class method name and derived class method name
have same name.
class Student
{
void message()
{
System.out.println("Good Morning Sir");
}
}
void display()
{
message();//will invoke or call current class message() method
super.message();//will invoke or call parent class message() method
}
}
class SuperEx1
{
public static void main(String args[])
{
Faculty s=new Faculty();
s.display();
}
}
In the above example Student and Faculty both classes have message()
method if we call message() method from Student class, it will call the
message() method of Student class not of Person class because priority of
local is high.
The super keyword can also be used to invoke or call the parent class
constructor. Constructor are calling from bottom to top and executing from
top to bottom.
To establish the connection between base class constructor and derived class
constructors JVM provides two implicit methods they are:
Super()
Super(...)
super()
super() It is used for calling super class default constructor from the context
of derived class constructors.
Super keyword used to call base class constructor
Syntax
class Employee
{
Employee()
{
System.out.println("Employee class Constructor");
}
}
class HR extends Employee
{
HR()
{
super(); //will invoke or call parent class constructor
System.out.println("HR class Constructor");
}
}
class SuperEx2
{
public static void main(String[] args)
{
HR obj=new HR();
}
}
Interface in Java
properties of Interface
Syntax of Interface
To declare interface we use the keyword interface
interface Interface_Name
{
public static final datatype variable_name=value;
public abstract returntype method _name(parameter list……);
}
An Inteface cannot be
A class can be instantiated i.e, objects of a instantiated i.e, objects
class can be created. cannot be created.
It cannot contain
It can contain constructors. constructors.
Variables in a class can be static, final or All variables are static and
non final. final.
Declaring Interfaces:
Explanations
public static final datatype variable name=value; ----> for data member
public abstract returntype methodname(parameters)---> for method
Inheriting an Interfaces:
interface Person
{
void run();
}
class Employee implements Person
{
public void run()
{
System.out.println("Run fast");
}
}
Employee(class)
interface Developer
{
void disp();
}
interface Manager
{
void show();
}
Interface B
Class A