Java: Class inheritance and member variable inheritance

The concept of inheritance in Java is a core concept that provides the key features of object-oriented programming: module reuse and code simplicity.

Java uses the concept of inheritance to construct a hierarchy of classes. The hierarchy of this class inherits from the classes provided by the JDK.

In Java, every class has a superclass, and the top class of all classes is the java.lang.Object class.

To specify an ancestor class for inheritance, use the keyword “extends”. Classes with inheritance can be declared as follows:

[public/final/abstract] class ClassName extends ParentClassName {
	...
}
class A {
	int aa = 1;
}

class B extends A {
	int bb = 2;
}

class C extends B {
	int cc = 3;
}

class Dabc {
	public static void main(String args[]) {
		C objc = new C();
		System.out.println("The value of the object attribute variable 'aa' of the objc object is " + objc.aa);
		System.out.println("The value of the object attribute variable 'bb' of the objc object is " + objc.bb);
		System.out.println("The value of the object attribute variable 'cc' of the objc object is " + objc.cc);
	}
}
Java inheritance example 1
class A {
	int i;
	private int j;

	void setij(int x, int y) {
		i = x;
		j = y;
	}
}

class B extends A {
	int total;

	void sum() {
		total = i + j;
		//An error occurred. Subclass can not use variable j of private type
	}
}

class Inheritance1 {
	public static void main(String args[]) {
		B subob = new B();
		subOb.sum();
	}
}
Java inheritance example 2
class B1 {
	int x;
}

class B2 extends B1 {
	String x;
}

class Inheritance2 {
	public static void main(String args[]) {
		B2 b2 = new B2();
		b2.x = "Java that is easy to understand";
		//Int x declared in class B1 is masked
		//If you try to put b2.x = 50, I get an error

		System.out.println("The x value contained in object b2 : " + b2.x);
		B1 b1 = new B1();
		b1.x = 5000;
		System.out.println("The x value contained in object b1 : " + b1.x);

	}
}
Java inheritance example 3
//Class variables can also be inherited
//When a class variable of an ancestor class is defined in a subclass, the variable of the ancestor class is hidden.
class C1 {
	static int x;
	static int y;
}

class C2 extends C1 {
	static String x;
}

class Inheritance3 {
	public static void main(String args[]) {
		C2.x = "Java that is easy to understand";
		//Int x declared in class C1 is masked
		C1.y = 20000;
		C1.x = 30000;

		System.out.println("The value contained in the class variable c2.x : " + c2.x);
		System.out.println("The value contained in the class variable c2.y(inherited from C1): " + c2.y);
		System.out.println("The value contained in the class variable c1.x: " + c1.x);
	}
}
Java inheritance example 4

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • 8 things you need to think about for work-life balance
    View on Youtube Image: juliezhuo.com who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object
  • Member variables in Java
    Java’s member variables are all declared outside the methods in the class and represent the properties that the object can have. Member variables consist of