In this case, class Animal is a superclass of class Herbivore and Carnivore. Herbivore and Carnivore class itself acts as a subclass. However, as you can see above, a subclass can be a superclass for other derived classes. In this case, subclass Herbivore can be a superclass of class Cow and Sheep. Likewise subclass Carnivore, can be a superclass of class Lion and Tiger.
Making Subclass
Java provides keyword extends that used to perform class derivation. The general form of the use of the keyword as follows:
class subclass-name extends superclass-name {
class body
}
Consider the following simple program, which will show how to create a derived class and any benefits from the process.
class A { private int a; public void setA (int value) { a = value; } public int getA() { return a; } } //create subclass from class A class B extends A { private int b; public void setB(int value) { b = value; } public int getB() { return b; } } class InheritanceDemo1 { public static void main(String[] args) { B obj = new B(); obj.setA(100); obj.setB(200); System.out.println("nilai a : " + obj.getA()); System.out.println("nilai b : " + obj.getB()); } }
As you can see in the code above, class B is a subclass or an instance of class A. As a result, class B will inherit the properties contained in the class A. As a proof of this statement, observe the return code above. There, we create an object or an instance of class B, but in fact, the object also has method setA() and getA(), but we never define both method in class B. This means, class B has inherited method setA() and getA() of class A. However, private data contained in the superclass (class A) remains inaccessible to the subclass (class B). When executed, the above program will give the following results:
Value a : 100
Value b : 200
Derived classes can be derived again into other new classes. For example in this case, we want to derived class B into a new class, namely the class C. In this way, the class C will inherit the properties contained in the class A and class B. The following code will demonstrate this:
class A { private int a; public void setA (int value) { a = value; } public int getA() { return a; } } //create subclass from class A class B extends A { private int b; public void setB(int value) { b = value; } public int getB() { return b; } } //create subclass from class B class C extends B { private int c; public void setC(int value) { c = value; } public int getC() { return c; } class InheritanceDemo2 { public static void main(String[] args) { C obj = new C(); obj.setA(100); obj.setB(200); obj.setC(300); System.out.println("Value a : " + obj.getA()); System.out.println("Value b : " + obj.getB()); System.out.println("Value c : " + obj.getC()); } }
This time, the result that will be provided by the program is as follows:
Value a : 100
Value b : 200
Value c : 300
From these results, we can see clearly that the object of class C will have the methods defined in the class A and B, namely: method setA(), getA(), setB(), and getB().
Unlike C++, Java does not support multiple inheritance. In C++, a derivative class may be formed from two or more parent classes. The derived class will then have the properties contained in all of the parent class.
Thank you so much for the valuable info
ReplyDeleteHere am sharing some information about Best Training Institutes java training in hyderabad |java course in hyderabad | java institutes in hyderabad | software training institutes in hyderabad |selenium training in hyderabad | software training institutes in hyderabad with placements |python training in hyderabad | python course in hyderabad | python institutes in hyderabad
Thank You for Sharing valuable information with us.
ReplyDeleteE-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.
For more details please visit our website : Aptech Janakpuri
Thanks for sharing this blog is very helpful for us. Java Course in Janak puri
ReplyDeleteThanks for sharing this article. This is very helpful. Java Course in Janakpuri.
ReplyDeleteSAP S/4HANA provides businesses with a wide range of collaboration options that can help them streamline their operations, reduce costs, and improve customer satisfaction. From procurement and finance to supply chain and sales, SAP S/4HANA offers a comprehensive set of collaboration tools that can help businesses stay ahead of the competition and drive growth in the digital age. For more information, visit our website SAP Consulting Services or call us @ 1-289-952-8845.
ReplyDeletekuşadası
ReplyDeletemilas
çeşme
bağcılar
zonguldak
İ3BLST