0% found this document useful (0 votes)
2 views24 pages

Java 2nd Sem

The document discusses key concepts of inheritance in Java, including single inheritance, method overriding, and multilevel inheritance, with examples illustrating each concept. It also explains abstract and final classes, detailing their purposes and restrictions, and provides guidance on creating and using packages in Java. Additionally, it lists various API packages available in Java, highlighting their contents and functionalities.

Uploaded by

kamalagowdab2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

Java 2nd Sem

The document discusses key concepts of inheritance in Java, including single inheritance, method overriding, and multilevel inheritance, with examples illustrating each concept. It also explains abstract and final classes, detailing their purposes and restrictions, and provides guidance on creating and using packages in Java. Additionally, it lists various API packages available in Java, highlighting their contents and functionalities.

Uploaded by

kamalagowdab2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
ook on Java mn Magy, ag de rere arks Questions carrying 4 oF more m eritance with Explain single inl n suitable example. 4, Explal a, + AHe mechanism inheritance. 4 The old class is kno parent class and the class or child class. of deriving @ new class from an old ong a Cal % wr as the base class'or super ci, 8 new one is called the subclass oy g 8 letiyg Inheritance Basics + Toinherit aclass, you simply incorporate the'definition of One gy into another by using the extends keyword. lag + Tosee how, let's begin with a short example. + The following program creates a superclass called 4 and subclass called B. Notice how the keyword extends is Used create a subclass of A. 1A simple example of inheritance. 1 Create a superclass. class A t int i,j; : void showij() { ' [Link]('i and j: “+i 4 "4 i); } Create a subclass by exten Class B extends A { int k; Void showk() ding class A. System out printing: “4K ys | Se me eee en eg age «0 de et Oriented Concepts Using Java aie — — ii oid sum() { [Link]("isjsk: "+ (iajsk)); ? } class Simpleinheritance { : public static void main(String args{}) { A superOb B subOt new AQ); = new BY); 1 The superclass may be used by itself. superOb.i = 1 superObj [Link]("“Contents of superOb: [Link](); [Link]-printin(); I" The-subclass has access to all public its superclass. */ subOb.i = 7; subObj = 8; subOb.k = 9; [Link](“Contents of subOb: [Link](); [Link](); i ‘[Link](); 2 members of [Link](“Sum of i, j and k in subOb:"); subOb,sum(); The output Contents of superOb: iand j: 10 20 A Book on Java hag Contents of subOb: iandj:78 ‘ k9 ‘ Sum of i, j and k in subOb: lajak: 24 Explain method overriding with suitable example, + When a method in a subclass has the si ame name gn, signature as @ method ‘in its superclass, then the Method in ti Subclass is said to overtide the method in the Superclass, When an overridden Method is called f always refer to the version of that method defined by the sabe’ The version of the method defined by the Superclass i hidden. Consider the following: 41 Method overriding, class A { inti, j; Alinta, int b) from within a Subclass, + W display i and j void show()

You might also like