Your ultimate guide

Java & C#_Object Oriented Programing2

Java & C#_Object Oriented Programing2


Inheritance:
  • When one object acquires all the properties and behaviours of the parent object, it is known as inheritance.
  • Inheritance represents the IS-A relationship, also known as the parent-child relationship.
  • It uses the 'extends' keyword to call the super or parent class.
Types of Inheritance:
  1. Single Level Inheritance
  2. Multi Level Inheritance
  3. Hierarchical Inheritance




    Single Level Inheritance:
    • In Single Level Inheritance, a child class can access the parent class, but the parent class can’t access the child class.
    • By using the 'extends' keyword we can connect the child class to the parent class.



        Example:

        public class ParentClass {
           public void student() {
               int id = 123;
               String name = "Testing Colleges";
               System.out.println(id+" "+name);
           }
        }

        ---------------------------------

        public class ChildClass extends ParentClass {
           public static void main(String[] args) {
               //Creating Object for child class
               ChildClass cc = new ChildClass();
               cc.student();
           }
        }

        Description: The provided code represents a basic example of Java class inheritance. Let's break down what's happening:
  1. ParentClass is a Java class that defines a method called student(). Inside this method, it declares two variables: an integer id with a value of 123, and a string name with the value "dollar". It then prints out the combination of id and name.
  2. ChildClass is another Java class that extends (inherits from) the ParentClass. This means that ChildClass inherits the student() method from ParentClass.
  3. Inside the main method of ChildClass, an instance of ChildClass is created with the variable cc. Then, the student() method of the ChildClass instance cc is called.
  4. When you run the ChildClass's main method, it creates an instance of ChildClass and calls the student() method inherited from ParentClass. As a result, the output will be: 123 Testing Colleges




    Multi-Level Inheritance:
    • Here Child class can access the parent class and super class.
    • Parent class can only access super class.
    • By using the 'extends' keyword we can connect the child class to the parent class and also the parent class to the super class.


    Example:
    public class SuperClass {
        public void display() {
            System.out.println("I am Super class");
        }
    }
    
    ---------------------------------
    public class ParentClass extends SuperClass {
        public void display2() {
            System.out.println("I am Parent class");
        }
    }

    ---------------------------------
    public class ChildClass extends ParentClass {
        public static void main(String[] args) {
            ChildClass cc = new ChildClass();
            cc.display2();
            cc.display();
        }
    }




    Hierarchical Inheritance:
    • In the Hierarchical inheritance, multiple Child classes can access one parent class.
    • By using extends keyword we can connect the child class to the parent class.
    
    Example:
    public class ParentClass {
        void display() {
            System.out.println("I am Parent Class");
        }
    }

    ---------------------------------

    public class ChildClass1 extends ParentClass {
        public static void main(String[] args) {
            ChildClass1 cc1 = new ChildClass1();
            cc1.display();
        }
    }

    ---------------------------------

    public class ChildClass2 extends ParentClass {
        public static void main(String[] args) {
            ChildClass2 cc2 = new ChildClass2();
            cc2.display();
        }
    }




Constructor:
  • Constructor is a special type of method.
  • The constructor is used to initialise the class variables.
  • The constructor should be the same as the class name.
  • The constructor will not return any value (not even void).
  • The constructor will be invoked at the time of object creation (we don’t need to call the constructor).

    Example:
    1. public class Testing {
    2.     int id;
    3.     String Name;
    4.     void display() {
    5.         System.out.println(id+" "+Name);
    6.     }
    7.     //Creating Constructor
    8.     Testing(int i, String n) {
    9.         id = I;
    10.         Name = n;
    11.     }

    12.     public static void main(String[] args) {
    13.     //Constructor will be invoked at the time of object creation.
    14.         Testing obj = new Testing(10, "Dollar");
    15.         obj.display();
    16.     }
    17. }
        -------------------------------
There are two types of Constructors:
  1. Default Constructors (no-arg constructor)
  2. Parameterized Constructors

    Default Constructors (no-arg constructor): A Constructor that doesn’t have any parameter is called Default Constructor.

Example:
    1. public class DefaultConstructor {
    2.     int x,y;
    3.     DefaultConstructor() {
    4.         x=10;y=20;
    5.         System.out.println(x+y);
    6.     }
    7.     public static void main(String args[]) {
    8.         DefaultConstructor dc = new DefaultConstructor();
    9.     }
    10. }

    Parameterized Constructors: A constructor that has a specific number of parameters is called a Parameterized Constructor.

Example:
    1. public class ParemeterizedConstructor {
    2.     int x,y;
    3.     ParemeterizedConstructor(int a, int b) {
    4.         x=a;
    5.         y=b;
    6.     }
    7.     void display() {
    8.         System.out.println(x+" and "+y);
    9.     }
    10.     public static void main(String args[]) {
    11.         ParemeterizedConstructor pc = new ParemeterizedConstructor(10,20);
    12.         pc.display();
    13.     }
    14. }






No comments:

Post a Comment