-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance.java
51 lines (43 loc) · 1.69 KB
/
Inheritance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Inheritance: Classes can be derived from classes. Basically, if you need to create a new class and here is already a class that has some of the code you require, then it is possible to derive your new class from the already existing code. This concept allows you to reuse the fields and methods of the existing class without having to rewrite the code in a new class.
* Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).
* Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
* Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
*
*/
/*
* Single Inheritance: Subclasses inherit the features of one superclass.
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Inheritance
{
public static void main(String[] args)
{
Child c = new Child();
c.display();
c.print_class_name();
Parent p = new Parent();
p.print_class_name();
}
}
class Parent
{
public void print_class_name()
{
System.out.println("This is from parent class.");
}
}
class Child extends Parent
{
@Override
public void print_class_name()
{
System.out.println("This is from child class.");
}
public void display()
{
System.out.println("Message from child class.");
}
}