Docsity

Inheritance and Abstract Classes in Java, Slides of Java Programming

Partial preview of the text

Download Inheritance and Abstract Classes in Java and more Slides Java Programming in PDF only on Docsity!

 Inheritance: Inheritance is a process, through which we can create a new class from an existing

class.

 Types:- 1.Single 2.Multilevel 3.Multiple 4.Hierarchical

 1.Single Inheritance:

 class A

 //code

 class B extends A

 //code

A

B

docsity.com

 Inheritance: Inheritance is a process, through which we can create a new class from an

existing class.

 Types:-1.Single 2.Multilevel 3.Multiple 4.Hierarchical

 2.Multilevel Inheritance:

 class A

 //code

 class B extends A

 //code

 class C extends B

 //code

A

B

C

docsity.com

 Inheritance: Inheritance is a process, through which we can create a new class from an existing class.

 Types:-1.Single 2.Multilevel 3.Multiple 4.Hierarchical

 3.Multiple Inheritance: Not supported by Java.

A

B

C D

docsity.com

 Inheritance: Inheritance is a process, through which we can create a new class from an existing class.

 Types:-1.Single 2.Multilevel 3.Multiple 4.Hierarchical

 3.Multiple Inheritance: Not supported by Java.

 Diamond Problem…But supported by C++ To Overcome this problem we have the concept of Interface..

A

B

C D

docsity.com

 SIMPLE EXAMPLE OF INHERITANCE

 class Furniture

 int height;

 int width;

 int length;

 class Table extends Furniture

 int no-of-legs;

 void init()

 height=10;

 width=20;

 length=30;

docsity.com

 no-of-legs=4;

 void display()

 System.out.println(―height=‖+height);

 System.out.println(―width=‖+width);

 System.out.println(―length=‖+length);

 System.out.println(―Number of Legs=‖+no-of-legs);

 public static void main(String arr[])

 Table t1=new Table();

 t1.init();

 t1.display();

adrish.b@ardentcollaboratio

docsity.com

 static keyword

 There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static , it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static because it must be called before any objects exist.

 If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:

class UseStatic

{ static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static //Static Block

{ System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[])

{ meth(42); }

}

Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non- static methods through object reference variables. A static variable can be accessed in the same way—by use of the dot operator on the name of the class. This is how Java implements a controlled version of global functions and global variables.

docsity.com

 static keyword

 There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static , it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static because it must be called before any objects exist.

 If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:

class UseStatic

{ static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static //Static Block

{ System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[])

{ meth(42); }

}

Here is an example. Inside main( ) , the static method callme( ) and the static variable b are accessed outside of their class class StaticDemo { static int a = 42; static int b = 99; static void callme() { System.out.println("a = " + a); } } class StaticByName { public static void main(String args[]) { StaticDemo.callme(); System.out.println("b = " + StaticDemo.b); } }

Here is the output of this program: a = 42 b = 99

docsity.com

this keyword:

 this keyword is used to refer to the current object.

 ‗this‘- is always a reference to object on which the method was invoked.

 Example:

 class Box{

 double width;

 double height;

 double depth;

 Box(double w,double h,double d) {

 width=w;

 height=h;

 depth=d;

 }

 double volume() {

 return widthheightdepth;

 }

 }

 class BoxDemo{

 public static void main(String arr[]) {

 Box mybox1=new Box(10,20,30);

 Box mybox2=new Box(3,6,9);

 double vol;

 vol=mybox1.volume();

 System.out.println("Volume is"+vol);

 vol=mybox2.volume();

 System.out.println("Volume is"+vol);

 }

 }

Output:

Volume is6000.

docsity.com

this keyword:

 this keyword is used to refer to the current object.

 ‗this‘- is always a reference to object on which the method was invoked.

 Example:

 class Box{

 double width;

 double height;

 double depth;

 Box(double w,double h,double d) {

 this.width=w;

 this.height=h;

 this.depth=d;

 }

 double volume() {

 return widthheightdepth;

 }

 }

 class BoxDemo{

 public static void main(String arr[]) {

 Box mybox1=new Box(10,20,30);

 Box mybox2=new Box(3,6,9);

 double vol;

 vol=mybox1.volume();

 System.out.println("Volume is"+vol);

 vol=mybox2.volume();

 System.out.println("Volume is"+vol);

 }

 }

Output:

Volume is6000.

docsity.com

 super: super keyword has two general forms

 It calls superclass constructor.

 used to access a member of the superclass that has been hidden by member of a subclass.

docsity.com

 METHOD OVERRIDING:

 Method Overriding is a process through which a base class method overridden by a derived class method

 For method Overriding Method name and signature should be same.

 class Furniture{

 int height;

 int width;

 int length;

 Furniture(int height,int width,int length) {

 this.height=height;

 this.width=width;

 this.length=length;

 void display(){

 System.out.println("height"+height);

 System.out.println("width"+width);

 System.out.println("length"+length);

docsity.com

Difference between Method Overloading and Method Overridiing:

Preventing method Inheritance and Overriding:

 Using final keyword

 final class A{

 //Code

 }

 class B extends A{

 //Code

 }

 Java runtime System will generate an Error:

 Can not inherit

 for any method we can apply final keyword to prevent overriding

 final void show(){

 //code

 }

Method overloading:

Compile-time Polymorphism

Early/Static Binding

Within one class

Method Overriding: Runtime Polymorphism Late/Dynamic Binding Base class and Child class

docsity.com

Abstract Class and Interfaces

By

Adrish Bhattacharyay

Ardent Collaborations

http://www.ardentcollaborations.com

[email protected]

docsity.com