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

 

Semantic Data Model Chapter No 3 RDBMS

  Chapter No 3 ⦁ Semantic Data Model ⦁ Relational Model ⦁ Database Models and Internet Semantic Data Model: "A semantic data model is a method of organizing data in a logical and meaningful way. " It provides a conceptual representation of data and the relationships between them, adding a layer of semantic information that gives data a basic meaning. Key Elements: 1.    - Entities: Represent objects or concepts (e.g., Person, Product). 2.    - Attributes: Characteristics or properties of entities. 3.    - Relationships: Connections between entities, defining associations through the foreign key. A semantic data model describes data about its real-world interpretation and usage.  For example, the object "Person" can be generalized to include "Employee," "Applicant," and "Customer," and is related to "Project" and "Task." A person can own multiple projects, and a specific task can be associated with differe...

Chapter No 2 Database System DAE CIT 2nd year

Chapter No. 2  Database System Legacy DB Systems    -  File Processing Systems    - Hierarchical Model    - Network Model Legacy Database Systems Legacy database systems played a crucial role in the evolution of data management, paving the way for more advanced relational database systems. Legacy Database Systems, including File Processing Systems Hierarchical Model Network Model,. 1. File Processing Systems Definition: File Processing Systems are traditional data management systems that use flat files to store and manage data. In this approach, each department or user group maintains its own set of files, leading to data redundancy. Characteristics: Data stored in flat files. No centralized control or structure. Limited data sharing and integration. Advantages: Simplicity: Easy to implement and understand. Independence: Each department has control over its data. Cost: Relatively low implementation and maintenance costs. Performance: Can be effic...