MZ Chapter - 3 - Inheritance and Interfaces Part One Oop
MZ Chapter - 3 - Inheritance and Interfaces Part One Oop
members, you can designate that the new class should inherit the
class, and the new class is the subclass | derived class | child
class.
Inheritance cont.
A subclass can add its own fields and methods.
The subclass exhibits the behaviors of its superclass
and can modify those behaviors so that they operate
appropriately for the subclass.
This is why inheritance is sometimes referred to as
specialization.
The direct superclass is the superclass from which the
subclass explicitly inherits.
An indirect superclass is any class above the direct
superclass in the class hierarchy, Which defines the
inheritance relationships between classes.
In Java, all classes are part of a class hierarchy that begins with the
Object class, which is defined in the java.lang package
The Object class is the root of the Java class hierarchy, and it defines
the basic behaviour and methods that are common to all objects
in Java.
Every class in Java implicitly inherits from the Object class, either
directly or indirectly.
If a class does not explicitly extend any other class, it automatically
extends the Object class. This means that every class in Java has access
to the methods and fields defined in the Object class, such as getClass(),
equals(), and hashCode().
Generalization and Specialization
– Inheritance represents the IS-A relationship which is also
known as a parent-child relationship.
• When a class represents the shared
characteristics of more than one class, it is
called a generalization; for example,
Vehicle is a generalization of Bike, Car,
and Truck.
• Similarly, when a class represents a
special instance of a general class, it is called a
specialization, so a Car is a
Why we use inheritance in java
1.Code reuse: Inheritance allows developers to reuse code from
existing classes, reducing the amount of code that needs to be
written. By inheriting properties and methods from a superclass, a
subclass can reuse and build upon the functionality of the
superclass without having to duplicate code.
2.Polymorphism: Inheritance is a key component of polymorphism,
which is the ability of objects to take on different forms.
Inheritance allows objects to inherit properties and methods from a
superclass, but then customize or override them as needed. This
flexibility allows developers to create objects with different
Why we use inheritance in java
Bonus: float
class Employee{ public class ProgrammerDemo extends Employee {
float bonus;
String firstName;
String specialty;
String lastName; public String getSpecialty() {
int age; return specialty;
float salary; }
public String getFirstName() { public void setSpecialty(String specialty) {
return firstName; this.specialty = specialty;
}
}
public void setFirstName(String firstName) { public float getBonus() {
this.firstName = firstName; return bonus;
} }
public String getLastName() { public void setBonus(float bonus) {
return lastName; this.bonus = bonus;
}
}
static void print(ProgrammerDemo prog){
public void setLastName(String lastName) { System.out.println(prog.getFirstName()+"\t"+
this.lastName = lastName; prog.getLastName()+"\t"+
} prog.getSpeciality()+"\t"+
public int getAge() { prog.getAge()+"\t"+
return age; prog.getSalary()+"\t"+
prog.getBonus());
}
}
public void setAge(int age) { public static void main(String[] args) {
this.age = age; ProgrammerDemo pObj = new ProgrammerDemo();
} pObj.setFirstName(“Dawit");
public float getSalary() { pObj.setLastName("Dagim");
return salary; pObj.setSpeciality("Mobile");
pObj.setAge(32);
}
pObj.setSalary(23,000);
public void setSalary(float salary) { pObj.setBonus(3,000);
this.salary = salary; print(pObj);
} }
} }
Types of inheritance in java
– On the bases of class, there can be three types of
inheritance in java
Hierarchical Inheritance
Multiple inheritance is not supported in java through class.
However, we can achieve multiple inheritance using
interfaces.
Hierarchical Inheritance example
Superclasses and Subclasses
Often, an object of one class is an object of another class as well.
Super classes tend to be “more general” and subclasses
“more specific”.
For example, a CarLoan is a Loan as are
HomeImprovementLoans and MortageLoans.
Thus, in Java, class CarLoan can be said to inherit from class
Loan.
In this context, class Loan is a superclass and class CarLoan is a
subclass.
Because every subclass object is an object of
its superclass, and one superclass can have
many subclasses,
subclass.
A superclass’s private members are not accessible
outside the class itself.
Rather, they’re hidden in its subclasses and can be
accessed only through the public or protected
methods inherited from the superclass.
Constructors in Subclasses
Instantiating a subclass object begins a chain of
argument constructor.
Constructors in Subclasses cont.
Similarly, if the superclass is derived from another class
as if, of course, every class except Object, the
superclass constructor invokes the constructor of the
next class up the hierarchy, and so on.
The last constructor invoked in the chain is always the
constructor for class Object.
The original subclass constructor’s body finishes
executing last.
Output
Constructors’ Rules in Inheritance
- If we have only default constructor in the parent class,
o We can avoid using supper call in child class; So, no need
to have any constructor in child class.
o The supper class default constructor will always be invoked
when object of child class is created implicitly default
constructor of child class.
Rule Conn…
- If we have only parametrized constructor in supperclass two
things are must.
– 1st it is must to have constructor in child class
– 2nd it is must to use supper call in all subclass’ constructors
• We cannot use two supper call in one constructor.
32
Output
• this to invoke current class method
class Adder{
static int add(int a, int b) {return a+b;}
static double add(double a, double b){return a+b;}
}
class AdderOverloadingTest{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.5,12.5));
}
}
Method Overloading example: By changing number
of arguments
class Adder {
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c){
return a + b + c;
}
}
public class AdderOverloadingTest {
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}
3.3 Method Overriding
Declaring a method in subclass which is already present in
parent class is known as Method overriding.
When overriding is done a child class give its own
implementation to a method which is already provided by the
parent class.
In this case ,
Output
• Syntax
super.methodName();
Using
class Animal {
super
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class
method
Super System.out.println("Dogs can walk and run");
Class }
Method }
public class TestDog {
Call public static void main(String args[]) { Animal
Animal b = new Dog(); referenc
b.move(); // runs the method in Dog class e
}
}
Using
super
Final Classes and Final Methods
The final keyword in java is used to restrict the user.
The java keyword can be used in many context.
Final can be:
Variable
Method
Class