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:
- Single Level Inheritance
- Multi Level Inheritance
- 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:
- 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.
- ChildClass is another Java class that extends (inherits from) the ParentClass. This means that ChildClass inherits the student() method from ParentClass.
- 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.
- 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:
- public class Testing {
- int id;
- String Name;
- void display() {
- System.out.println(id+" "+Name);
- }
- //Creating Constructor
- Testing(int i, String n) {
- id = I;
- Name = n;
- }
- public static void main(String[] args) {
- //Constructor will be invoked at the time of object creation.
- Testing obj = new Testing(10, "Dollar");
- obj.display();
- }
- }
-------------------------------
There are two types of Constructors:
- Default Constructors (no-arg constructor)
- Parameterized Constructors
Example:
- public class DefaultConstructor {
- int x,y;
- DefaultConstructor() {
- x=10;y=20;
- System.out.println(x+y);
- }
- public static void main(String args[]) {
- DefaultConstructor dc = new DefaultConstructor();
- }
- }
Parameterized Constructors: A constructor that has a specific number of parameters is called a Parameterized Constructor.
Example:
- public class ParemeterizedConstructor {
- int x,y;
- ParemeterizedConstructor(int a, int b) {
- x=a;
- y=b;
- }
- void display() {
- System.out.println(x+" and "+y);
- }
- public static void main(String args[]) {
- ParemeterizedConstructor pc = new ParemeterizedConstructor(10,20);
- pc.display();
- }
- }
No comments:
Post a Comment