Skip to main content

Chapter No 5 Object Oriented Programming (OOP)Java

 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

Popular posts from this blog

DAE CIt 244 Electronics 2ndyear Past Paper 2019

 

Chapter No 2 Java DAE CIT

  . Variable and Operators 1.1. Variable: Define: A variable is a named storage location in a program that holds data, and its value can be changed during the execution of the program. Syntax: datatype variableName; Explanation: Variables are used to store and manipulate data in a program. They have a data type that defines the kind of data they can hold, such as int, float, char, etc. Code int age; // Declaration of an integer variable age = 25; // Initialization of the variable with a value 1.2. Create Instance of a Variable: Define: Creating an instance of a variable involves declaring and optionally initializing a variable in a program. Syntax: datatype variableName = value; Explanation: This creates a variable and assigns an initial value to it at the time of declaration. score = 95.5  # Declaration and initialization of a floating-point variable 1.3. Use Preemptive Data Types: Define: Preemptive data types are data types that are predefined and provided by...

Chemistry Past papers of DAE CIT 1st year

1. **What are Derived units? Give examples.**    - Derived units are units of measurement that are derived from the base units of a system. They express quantities in terms of fundamental units. Examples include:      - Newton (N) for force (kg·m/s²)      - Joule (J) for energy (N·m)      - Watt (W) for power (J/s) 2. **Define Radical, Valency, Formula, and Chemical Equation.**    - Radical: A radical is a group of atoms bonded together that behaves as a single unit in chemical reactions.    - Valency: Valency is the number of chemical bonds an atom can form when it combines with other atoms in a compound.    - Formula: A formula is a representation of a chemical compound using symbols and subscripts to show the ratio of atoms in the compound.    - Chemical Equation: A chemical equation is a symbolic representation of a chemical reaction, showing the reactants and products with their respectiv...