Skip to main content

Hierarchical Inheritance In Java with Example Program

 

Hierarchical Inheritance In Java with Example Program

Hierarchical Inheritance

Inheritance which contain only one parent class and multiple child class, and all child class directly extends the parent class this is called hierarchical inheritance


For example, consider A, B and C as three classes: A parent class,B and C are two child classes.Here B & C extends to A class,Similarly object should be created separately for B & C class.So B&A class property can be accessed using B class object. C&A class property can be accessed using C class

object.







Hierarchical Inheritance in java example program Program


class Parent { int age1=50; void display1() { System.out.println("This is parent class method disply1"); System.out.println("Parent class accessing through both obj1 and obj2"); System.out.println("____________________________________"); } } class Bchild extends Parent { int age2=40; void display2() { System.out.println("This is Bchild method and method2"); System.out.println("First child class of class parent"); System.out.println("____________________________________"); } } class Cchild extends Parent { int age3=30; void display3() { System.out.println("This is Cchild class and method disply3"); System.out.println("Second child class of class parent"); System.out.println("____________________________________"); } } class Mm { public static void main(String a[]) { Bchild obj1=new Bchild();//obj1 is Bchild class object name Cchild obj2=new Cchild();//obj2 is Cchild class object name System.out.println("____________________________________"); obj1.display1();//will print parent method obj1.display2();//will print Bchild method obj2.display3();//will print Cchild method obj2.display1();//will print parent method System.out.println("parent age variable :"+obj1.age1); System.out.println("Bchild age variable :"+obj1.age2); System.out.println("Cchild age variable :"+obj2.age3); System.out.println("parent age variable :"+obj2.age1); System.out.println(); } }


observe the program and its output carefully



CMD output


CMD output of Java hierarchical Inheritance example program












Comments

Popular posts from this blog

Introduction to java programming-very important theory notes

Introduction to java programming what is java? Java is a high level object oriented computer programming language(OOPS). Java is a general purpose programming language used in computers and other applications. Java, an object-oriented programming language developed by Sun Microsystems under the leadership of James Gosling, Bill Joy, etc., is used today in many electronic devices such as web servers, computers, and mobile phones. Although it is mostly used for web programming, it is a multi-purpose programming language that can be used to do many other things. After Oracle bought Sun Microsystems in mid-2009, Java came under Oracle's control. (Wikipedia). What is a computer? A computer is an electronic device for storing and processing huge amounts of information. What is a computer program? A computer program is a set of instructions for a computer to perform a specific task. A computer cannot do anything by itself, if it wants to do something, it must get clear instructions, the i...

JAVA BUZZWORDS

     JAVA BUZZWORDS & H istory of the development of java java buzzwords explanation     Features of java is called as Java Buzzwords Platform independent —> Run on any Environment Object oriented concept(oops) —> Abstraction,Encapsulation,Inheritance and polymorphism Simple —>  No concept of pointers,explicit memory allocation, structures,operator overloading etc  Secure —> Secure for internet application (Raise  out of boundary exception in arrays and it is strong typed language) Robust —> Early checking of error it can achieved garbage collector, exception handling Portable  —> Java program written on one environment can be executed or implemented another environment Multithreading  —> Concurrent execution of several parts of same program at same time(This will improve CPU utilization) Distributed Application —> This are software that runs on multiple computers connected  to a network at ...

How to run java program using while loop

 Java program how to print Happy New Year in 10 times(using while loop) java buzzwords static keyword in java The AI ​​Revolution- Malayalam article step by step explanation Before writing Java code Install Jdk on your system and Set the path properly. Then type the Java code in the notepad and save it as a class name with the .java extension. Select the file type 'all file' while saving. Open cmd, Change the directory where the file is saved, using the cd command. Type javac class name.java then press the enter key, If you type java and give the class name, If there is no error in the program, the output will be generated. Observe the directory change using the cd command. Give the class name and watch it compile and run. How to run java program in notepad and cmd Where to code in Java and how it works? method 1 A text editor is required to write Java code The code can be run using the command prompt method 2  using an integrated development environment (IDE), such as Eclips...

Oops concepts in java

oops concepts in java with example programs Oops concepts in java Oops concept is very very important to make java programming easier and increase your programming skill.Four pillars of oops are Abstraction,Encapsulation,Inheritance,Polymorphism.Another technique to carefully understand in Java is classes and objects. This article covers very simple notes for Java programming beginners. What is object and class? The basics of oops is class and object Class is a blueprint which defines some properties and behaviors.  An object is an instance of a class which has those properties and behaviors attached.A class is not allocated memory when it is defined. An object is allocated memory when it is created Class and object —> object is defined as real word entities .Object consists of property and some task Class is a blueprint that object follows, Class  consists of many number of objects Four pillars of oops are Abstraction   —> Means showing only essential parts and hi...

Command Line Arguments In Java

Command Line Arguments In Java No input statement needed in the program itself. we can pass the input as an argument while running the program. Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command line arguments to main function. To access the command line arguments inside a java program is quite easy are stored as string in string array passed to main function. Example program of command line arguments in java class CommandLineExample {    public static void main(String args[]) {    System.out.println("\nMy first input is  :"+args[0]);    }    }    Addition of two numbers in java using command line arguments  Example:write a program add two integer number    class AddCommandLine { public static void main(String args[]) { int num1=Integer.parseInt(args[0]); //type cast int num2=Integer.parseInt(args[1]); //type ...

Keywords in java programming

Keywords in java programming Keyword is a predefined word given by the compiler which has some specific task. Keywords also known as reserved word. All the java technology keywords are lower case . Java programming has a richer set of keywords than C language or C ++ language. Some java keywords are import —>In java programming implementing classes and methods import the packages that purpose using the keyword import. class —> In order to create a class we have use the keyword class to see an example written using the class keyword ) this —> This keyword is used to represent the method belongs to particular current object Super —> It can used in inheritance concept extends —> Also used in inheritance concepts. Accessing methods one class to another class that can done by extends keyword package —> In java there will be package which consists of number of classes and interfaces in order to access that package, using the key word package return —> Return key wor...

Naming Convention In Java

Naming Convention In Java-camel case example Naming Conventions are rules to be followed by giving a name to the identifier.Identifiers are program elements(such as class,object,variable,method so on ).For building software you should follow this rule to make your code look good, more efficient and more readble.Java has certain rules for naming identifier. This are given below Digit, letters, underscore or currency symbols( £, €, $ ) are allowed                The first letter of any identifier cannot be a digit An identifier name must not be a reserved words or keywords Java follows camelCase rule what is camelcase rule? CamelCase is a way to separate the words in a phrase by making the first letter of each word capitalized and not using spaces (ex :- MyFirstJavaClass) Class name —> should begin with a capital letter(In a single word) If it is multiple words, the first letter of every word begins with a capital letter. Ex :- class Stu...