Java Class Inheritance

24blognews
1 min readNov 20, 2023

--

Inheritance in Java, is an inbuilt predefined feature comes inside the OOPS (object-oriented programming) concept. As the name itself speaks, getting access to something which is pre generated. Java Inheritance is categorized in to three types. Single, Multilevel and Hierarchical inheritance. Here we go with first one.
Single inheritance: Under this inheritance, the access to attributes and methods by a class of a super class is one way path only, which means sub class of a super class can only and only inherit properties of an only one super class.
For accessing the super class features extend keyword is used.
class mahadev { // super class
protected String movies = “matrix”; //super class mahadev attribute
public void mySuperClassMethod(){ // super class method
System.out.println(“matrix is a science fiction movie”);
}
}

class narayan extends mahadev { // extend keyword is used to access super class attributes and methods
private String movie_actor_names = “Thomas A Anderson”; //class narayan attribute
public static void main(String[] args){

narayan mySubClassObject = new narayan(); // creating subclass object
mySubClassObject.mySuperClassMethod(); // calling mySuperClassMethod method from super class

System.out.println(mySubClassObject.movies + “ :- “ + mySubClassObject.movie_actor_names);
}
}

Keep coding and visit the below link
https://www.roseindia.net/java/ #javaclassinheritance #inheritance #coding

--

--