Chapter No 5
Object Oriented Programming
5.1. Inheritance
5.2. Polymorphism
5.3. Encapsulation
5.1 Inheritance
Inheritance is a fundamental concept in object-oriented programming that allows a class (subclass/derived class) to inherit properties and behaviors from another class (superclass/base class).
Constructor cannot be inherited in Java.
Private members do not get inherited in Java.
Cyclic inheritance is not permitted in Java.
Assign parent reference to child objects.
Constructors get executed because of super() present in the constructor.
This promotes code reusability and establishes a hierarchical relationship between classes.
Syntax:
class Superclass {
// superclass members
}
class Subclass extends Superclass {
// subclass members
}
Syntax Explanation:
The extends keyword is used to indicate that one class is inheriting from another.
The subclass inherits the members (fields and methods) of the superclass.
The subclass can override methods from the superclass and provide its own implementation.
Programming Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
} }
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
} }
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // inherited from Animal
myDog.bark(); // specific to Dog
} }
Types of Inheritance :
There are three types of inheritance.
1.Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes it is also known as Simple inheritance.
Example
class Employee {
float salary=34534*12; }
public class Executive extends Employee {
float bonus=3000*6;
public static void main(String args[]) {
Executive obj=new Executive();
System.out.println("Total salary credited: "+obj.salary);
System.out.println("Bonus of six months: "+obj.bonus);
}}
2.Multilevel Inheritance:
When a class extends a class, which extends another class then this is called multilevel inheritance.
Example:
class Shape {
public void display() {
System.out.println("Inside display");
} }
class Rectangle extends Shape {
public void area() {
System.out.println("Inside area");
} }
class Cube extends Rectangle {
public void volume() {
System.out.println("Inside volume");
} }
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
} }
3.Hierarchical Inheritance:
If a number of classes are derived from a single base class, it is called hierarchical inheritance.
Example:
class Animal{
void eat(){System.out.println("eating...");}}
class Dog extends Animal{
void bark(){System.out.println("barking...");}}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat(); }}
5.2 Polymorphism
Polymorphism allows objects of different types to be treated as objects of a common type.
It is achieved through method overriding and interfaces, enabling a single interface to represent different underlying forms.
Syntax:
class Parent {
void display() {
System.out.println("This is the parent class."); } }
class Child extends Parent {
void display() {
System.out.println("This is the child class.");
} }
Syntax Explanation:
Polymorphism is often achieved through method overriding where a subclass provides a specific implementation of a method defined in its superclass. The same method name in different classes can exhibit different behaviors.
Programming Example:
public class Main {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
obj1.display(); // calls display() from Parent
obj2.display(); // calls display() from Child (polymorphism)
} }
5.3 Encapsulation
Encapsulation in Java is a process of wrapping code and data together into a single unit.It helps in hiding the internal details of an object and protects the integrity of the data.
Syntax:
class MyClass {
private int myField;
public int getMyField() {
return myField; }
public void setMyField(int value) {
myField = value;
} }
Syntax Explanation:
The private keyword is used to restrict direct access to the field from outside the class.
Getter and setter methods provide controlled access to the private field.
Programming Example:
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.setMyField(42);
System.out.println(obj.getMyField());
}
}
Comments
Post a Comment