Unit 25t
Unit 25t
Object Oriented
Programming in Java
Advantages of Inheritance:
1. Reusability: It is a reuse mechanism at
both the design and the programming
level. Code Reusability is one of the major
advantage of Inheritance.
2. It is an abstraction mechanism which may
be used to classify entities
3. The inheritance graph is a source of
organisational knowledge about domains
and systems
Class
A class can also be defined as a blueprint from which you can create
an individual object. Class doesn't consume any space.
Collection of objects is called class. It is a logical entity.
A class is a user defined blueprint or prototype from which objects are
created. It represents the set of properties or methods that are common
to all objects of one type.
We can think of the class as a sketch(prototype) of a house. It contains
all the details about the floors, doors, windows, etc. Based on these
description, we build the house. The house is an object.
class Bike{
int speed =50; //fields or variables
public void brake(){ //method
System.out.println(“Applying Brakes”);
}
}
Getter and setter methods can be used to get and set the values of an
object respectively.
The setter method takes one or more arguments and then sets the
values of those arguments to the variables/fields of object.
i.e. setters are used to initialize objects
Syntax:
public void setterName(dataType arg1, datatype arg2){
//code to set the value of received argument to the variables/fields of
objects
}
Prepared By: Er. Sudarshan Subedi
Getters and Setters
Output
Constructors must have the same name as the class within which it is
defined while it is not necessary for the method in Java.
Constructors do not return any type while method(s) have the return
type or void if does not return any value.
Constructors are called only once at the time of Object creation while
method(s) can be called any number of times.
In Java, two or more methods may have same name but differ in
parameters (different number of parameters, different types of
parameters, or both)
These methods are said to be overloaded methods and this feature is
called method overloading.
i.e. method overloading same method name but different parameters
Method Overloading in Java-Example
Method Overloading in Java-Example
Constructor Overloading in Java
Similar to methods overloading in Java, we can also create two or more constructors
with different parameters. This is called constructor overloading.
Method Overriding in Java
If the same method is present in both the superclass and subclass, the
method in the subclass overrides the method in the superclass
We can use the @Override annotation to tell the compiler that we are
overriding a method. However, the annotation is not mandatory.
If an object of a parent class is used to invoke the method, then the
version in the parent class will be executed, but if an object of the
subclass is used to invoke the method, then the version in the child
class will be executed.
Java Overriding Rules:
• Both the superclass and the subclass must have the same method name, the same
return type and the same parameter list.
• We cannot override the method declared as final and static.
• We should always override abstract methods of the superclass
Prepared By: Er. Sudarshan Subedi
Method Overriding in Java Inheritance-Example
Abstract class: is a restricted class that cannot be used to create objects (to
access it, it must be inherited from another class).
A class which contains the abstract keyword in its declaration is known as
abstract class.
Abstract classes may or may not contain abstract methods
• An abstract method is a method without body
But, if a class has at least one abstract method, then the class must be
declared abstract.
If a class is declared abstract, it cannot be instantiated.
To use an abstract class, we have to inherit it from another class, provide
implementations to the abstract methods in it.
Prepared By: Er. Sudarshan Subedi
Abstraction In Java-Abstract Class-Summary
Now any class that implements InterfaceB will have both the
implementation of both the methods.
The keyword used to create a class is “class” The keyword used to create an interface is “interface”
A class can be instantiated i.e., objects of a class can be created. An Interface cannot be instantiated i.e. objects cannot be created.
Classes do not support multiple inheritance. The interface supports multiple inheritance.
Variables in a class can be static, final, or neither. All variables are static and final.
Prepared By: Er. Sudarshan Subedi
Object Class In Java
Object class is present in java.lang package and class acts as a root
of inheritance hierarchy in any Java Program
Every class in Java is directly or indirectly derived from the Object
class.
If a Class does not extend any other class then it is direct child class
of Object and if extends other class then it is an indirectly derived.
Therefore the Object class methods are available to all Java classes.
1. toString()
It provides string representation of an
Object
The default toString() method for class
Object returns a string consisting of the
name of the class of which the object is an
instance, the at-sign character `@’, and
the unsigned hexadecimal representation
of the hash code of the object
It is always recommended to override
toString() method to get our own String
representation of Object
Prepared By: Er. Sudarshan Subedi
Some Methods In Object Class
2. hashCode()
For every object, JVM generates a unique number
which is hashcode.
It returns distinct integers for distinct objects.
A common misconception about this method is that
hashCode() method returns the address of object,
which is not correct.
It convert the internal address of object to an integer by
using an algorithm
Override of hashCode() method needs to be done such
that for every object we generate a unique number.
For example, for a Student class we can return roll no.
of student from hashCode() method as it is unique.
Prepared By: Er. Sudarshan Subedi
Some Methods In Object Class
3. equals(Object Obj)
Compares the given object to “this” object (the
object on which the method is called).
It gives a generic way to compare objects for
equality.
It is recommended to override equals(Object obj)
method to get our own equality condition on
Objects.
It is generally necessary to override the
hashCode() method whenever this method is
overridden, so as to maintain the general contract
for the hashCode method, which states that equal
objects must have equal hash codes.
Prepared By: Er. Sudarshan Subedi
Some Methods In Object Class
4. getClass()
Returns the class object of “this” object and used
to get actual runtime class of the object
As it is final so we don’t override it.
5. finalize()
This method is called just before an object is
garbage collected.
It is called by the Garbage Collector on an object
when garbage collector determines that there are
no more references to the object.
We should override finalize() method to dispose
system resources, perform clean-up activities and
Prepared By: Er. Sudarshan Subedi
minimize memory leaks
Some Methods In Object Class
6. clone()
• It returns a new object that is exactly the same as this object
7. The remaining three methods wait(), notify() notifyAll() are related
to Concurrency
Create a class named Vehicle that has a method named wheels that
displays “Vehicle has wheels”. Similarly create two classes Bike and
Car that override the wheels method and display the number of wheels
respectively. Illustrate the concept of dynamic method dispatch/ runtime
polymorphism. Ask number from user. Call wheels of Bike if user
enters 1, that of Car if user enters 2 and that of vehicle if user enters
other numbers.
An abstract class cannot be directly instantiated A concrete class can be directly instantiated using
using the new keyword. the new keyword.
An abstract class cannot be declared as final. A concrete class can be declared as final.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and Interface has only static and final variables.
non-static variables.
4) Abstract class can provide the implementation of Interface can't provide the implementation of abstract
interface. class.
5) The abstract keyword is used to declare abstract The interface keyword is used to declare interface.
class.
6) An abstract class can extend another Java class and An interface can extend another Java interface only.
implement multiple Java interfaces.
7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
8) A Java abstract class can have class members like Members of a Java interface are public by default.
private, protected, etc.
Prepared By: Er. Sudarshan Subedi
Class vs Interface
Class Interface
The keyword used to create a class is “class” The keyword used to create an interface is “interface”
A class can be instantiated i.e., objects of a class can be created. An Interface cannot be instantiated i.e. objects cannot be created.
Classes do not support multiple inheritance. The interface supports multiple inheritance
Variables in a class can be static, final, or neither. All variables are static and final.
Prepared By: Er. Sudarshan Subedi