Oops Chapter 4
Oops Chapter 4
Oops Chapter 4
Inheritance
The process of obtaining the data members and methods from one class to another class is
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.
from faculty class and they are visible in student class logically.
Advantage of inheritance
If we develop any application, using concept of Inheritance than that application have following
advantages,
Types of Inheritance
Based on number of ways inheriting the feature of base class into derived class we have the
fallowing types:
Single inheritance
Multiple inheritance
Single inheritance
In single inheritance there exists single base class and single derived class.
Multiple inheritance
In multiple inheritance, there exist multiple classes and single derived class.
The concept of multiple inheritance is not supported in java through concept of classes but it can
In order to inherit the feature of base class into derived class we use the following syntax
Syntax
Explanation
1. ClassName-1 and ClassName-2 represents name of the base and derived classes
respectively.
2. Extends is one of the keyword used for inheriting the features of base class into
derived class it improves the functionality of derived class.
In java, programming one derived class can extends only one base class because java
programming does not support multiple inheritance through the concept of classes,
but it can be supported through the concept of Interface.
When we create an object of bottom most derived class, first we get the memory space
for the data members of top most base class, and then we get the memory space for
data member of other bottom most derived class.
Bottom most derived class contains logical appearance for the data members of all top
most base classes.
If we do not want to give the features of base class to the derived class then the
definition of the base class must be preceded by final hence final base classes are not
reusable or not inheritable.
If we are do not want to give some of the features of base class to derived class then
such features of base class must be as private hence private features of base class are
not inheritable or accessible in derived class.
Data members and methods of a base class can be inherited into the derived class but
constructors of base class cannot be inherited because every constructor of a class is
made for initializing its own data members but not made for initializing the data
members of other classes.
An object of base class can contain details about features of same class but an object of
base class never contains the details about special features of its derived class (this
concept is known as scope of base class object).
For each and every class in java there exists an implicit predefined super class called
java.lang.Object. because it providers garbage collection facilities to its sub classes for
collecting un-used memory space and improved the performance of java application.
Example of Inheritance
class Faculty
{
float salary=30000;
}
class Science extends Faculty
{
float bonous=2000;
public static void main(String[] args)
{
Science obj=new Science();
System.out.println("Salary is:"+obj.salary);
System.out.println("Bonous is:"+obj.bonous);
}
}
Output
Due to ambiguity problem java does not support mutiple inheritance at class level.
Example
class A
{
void disp()
{
System.out.println("Hello");
}
}
class B
{
void disp()
System.out.println("How are you ?");
}
}
class C extends A,B //suppose if it were
{
Public Static void main(String[] args)
{
C obj=new C();
obj.disp();//Now which disp() method would be invoked?
}
}
In above code we call both class A and class B disp() method then it confusion which class
method is call. So due to this ambiguity problem in java do not use multiple inheritance at class
Whenever same method name is exiting multiple times in the same class with different number of
overloading.
In this example, we have created two overloaded methods, first sum method performs addition of
two numbers and second sum method performs addition of three numbers.
Example
class Addition
{
void sum(int a, int b)
{
System.out.println(a+b);
}
void sum(int a, int b, int c)
{
System.out.println(a+b+c);
}
public static void main(String[] args)
{
Addition obj=new Addition();
obj.sum(10, 20);
obj.sum(10, 20, 30);
}
}
Output
30
60
In this example, we have created two overloaded methods that differs in data type. The first sum
method receives two integer arguments and second sum method receives two float arguments.
Example
class Addition
{
void sum(int a, int b)
{
System.out.println(a+b);
}
void sum(float a, float b)
{
System.out.println(a+b);
}
public static void main(String[] args)
{
Addition obj=new Addition();
obj.sum(10, 20);
obj.sum(10.05, 15.20);
}
}
Output
30
25.25
Overriding
Whenever same method name is existing in both base class and derived class with same types
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
Example
class Walking
{
void walk()
{
System.out.println("Man walking fastly");
}
}
class Man extends walking
{
void walk()
{
System.out.println("Man walking slowly");
}
}
public static void main(String[] args)
{
Man obj = new Man();
obj.walk();
}
}
Output
highest priority is given to current class (derived class). We can see in the above example high
Overloading Overriding
Whenever same method or Constructor is Whenever same method name is existing
existing multiple times within a class either with multiple time in both base and derived class
1 different number of parameter or with different with same number of parameter or same
type of parameter or with different order of type of parameter or same order of
parameter is known as Overloading. parameters is known as Overriding.
Arguments of method must be different at least Argument of method must be same
2
arguments. including order.
3 Method signature must be different. Method signature must be same.
Private, static and final methods can be Private, static and final methods can not be
4
overloaded. override.
Access modifiers point of view not reduced
5 Access modifiers point of view no restriction.
scope of Access modifiers but increased.
Also known as compile time polymorphism or Also known as run time polymorphism or
6
static polymorphism or early binding. dynamic polymorphism or late binding.
Overloading can be exhibited both are method Overriding can be exhibited only at method
7
and constructor level. leve.
The scope of Overriding is base class and
8 The scope of overloading is within the class.
derived class.
Overloading can be done at both static and Overriding can be done only at non-static
9
non-static methods. method.
For overloading methods return type may or For overriding method return type should be
10
may not be same. same.
Abstract classes
Abstract class
A class that is declared with abstract keyword is known as abstract class. An abstract class is
one, which is containing some defined method, and some undefined method. In java
………..
Abstract method
An abstract method is one, which contains only declaration or prototype, but it never contains
body or definition. In order to make any undefined method as abstract whose declaration is must
Syntax
Example
Output
An object of abstract class cannot be created directly but it can be created indirectly. It means
you can create an object of abstract derived class. You can see in above example
Example
Abstract classes definitions should not be made as final because abstract classes
always participate in inheritance classes.
An object of abstract class cannot be created directly but it can be created indirectly.
All the abstract classes of java makes use of polymorphism along with method
overriding for business logic development and makes use of dynamic binding for
execution logic.
More performance
Example of abstract class having method body
Output
Mileage is 60 km/ltr..
Output
constructor is invoked
running safely..
40
Final keyword is used to make a variable as a constant. This is similar to const in other language.
The program cannot modify a variable declared with the final keyword after initialization. This is
Example
It makes a method final, meaning that sub classes cannot override this method. The compiler
checks and gives an error if you try to override the method.When we want to restrict overriding,
class Employee{
final void disp(){
System.out.println("Hello Good Morning");
}}
class Developer extends Employee{
void disp(){
System.out.println("How are you ?");
}
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 cannot be inheriting by other classes. When we
Output
Output:
It gives an error
CHAPTER 5
Interface
Interface is similar to class which is collection of public static final variables (constants) and
abstract methods.
The interface is a mechanism to achieve fully abstraction in java. There can be only abstract
methods in the interface. It is used to achieve fully abstraction and multiple inheritance in Java.
Properties of Interface
Each method in an interface is also implicitly abstract, so the abstract keyword is not
needed.
All the data members of interface are implicitly public static final.
Declaring Interfaces:
interface Person
{
datatype variablename=value;
//Any number of final, static fields
returntype methodname(list of parameters or no parameters)
//Any number of abstract method declarations
}
Explanations
In the above syntax Interface is a keyword interface name can be user defined name the default
signature of variable is public static final and for method is public abstract. JVM will be added
implicitly public static final before data members and public abstract before method.
Example
public static final datatype variable name=value; ----> for data member
Implementing Interfaces:
A class uses the implements keyword to implement an interface. The implements keyword
appears in the class declaration following the extends portion of the declaration.
Example
interface Person
{
void run();
}
class Employee implements Person
{
public void run()
{
System.out.println("Run fast");
}
}
A class can extend only one class, but implement many interfaces.
An interface can extend another interface, similarly to the way that a class can extend
another class.
interface Person
{
void run(); // abstract method
}
class A implements Person
{
public void run()
{
System.out.println("Run fast");
}
public static void main(String[] args)
{
A obj = new A();
obj.run();
}
}
Output
Run fast
interface Developer
{
void disp();
}
interface Manager
{
void show();
}
Output
Encapsulation
language encapsulation can be achieve using class keyword, state represents declaration of
Advantage of Encapsulation
The main advantage of using of encapsulation is to secure the data from other methods, when
we make a data private then these data only use within the class, but these data not accessible
side capsule.
Benefits of encapsulation
class Employee
{
private String name;
Output
Harry
Polymorphism in Java
Here original form or original method always resides in base class and multiple forms represents
Polymorphism is not a programming concept but it is one of the principal of OOPs. For many
implementations are varying from one objects oriented programming language to another object
Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many
Suppose if you are in class room that time you behave like a student, when you are in market at
that time you behave like a customer, when you at your home at that time you behave like a son
In java programming the Polymorphism principal is implemented with method overriding concept
of java.
sum() method which are present in DC1 and DC2 are called overridden form hence Sum()
method is originally available in only one form and it is further implemented in multiple forms.
In below example we create two class Person an Employee, Employee class extends Person
class feature and override walk() method. We are calling the walk() method by the reference
variable of Parent class. Since it refers to the subclass object and subclass method overrides the
Parent class method, subclass method is invoked at runtime. Here method invocation is
Syntax
class Person
{
void walk()
{
System.out.println("Can Run....");
}
}
class Employee extends Person{
void walk(){
System.out.println("Running Fast...");
}
public static void main(String[] args){
Person p=new Employee(); //upcasting
p.walk();
}
}
Output
Running fast...
Dynamic Binding
Dynamic binding always says create an object of base class but do not create the object of
derived classes. Dynamic binding principal is always used for executing polymorphic applications.
The process of binding appropriate versions (overridden method) of derived classes which are
inherited from base class with base class object is known as dynamic binding.
Advantages of dynamic binding along with polymorphism with method overriding are.
More performance
Static polymorphism
The process of binding the overloaded method within object at compile time is known as Static
polymorphism due to static polymorphism utilization of resources (main memory space) is poor
because for each and every overloaded method a memory space is created at compile time when
it binds with an object. In C++ environment the above problem can be solve by using dynamic
polymorphism by implementing with virtual and pure virtual function so most of the C++ developer
Dynamic polymorphism
In dynamic polymorphism method of the program binds with an object at runtime the advantage of
dynamic polymorphism is allocating the memory space for the method (either for overloaded
method or for override method) at run time.
Conclusion
The advantage of dynamic polymorphism is effective utilization of the resources, so java always
use dynamic polymorphism. Java does not support static polymorphism because of its limitation.