[go: up one dir, main page]

0% found this document useful (0 votes)
1K views40 pages

Interfaces and Abstract Class by Durga Sir

The document discusses various aspects of interfaces in Java including interface declaration and implementation, extends vs implements, interface methods and variables, naming conflicts between interfaces, and the differences between interfaces, abstract classes, and concrete classes. Key points covered include how interfaces are 100% abstract, interface methods are always public and abstract, variables in interfaces are public, static and final, and how interfaces can extend other interfaces but classes can only extend one superclass.

Uploaded by

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

Interfaces and Abstract Class by Durga Sir

The document discusses various aspects of interfaces in Java including interface declaration and implementation, extends vs implements, interface methods and variables, naming conflicts between interfaces, and the differences between interfaces, abstract classes, and concrete classes. Key points covered include how interfaces are 100% abstract, interface methods are always public and abstract, variables in interfaces are public, static and final, and how interfaces can extend other interfaces but classes can only extend one superclass.

Uploaded by

Lalit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Interfcae

1. Introduction
2. Interfaces declaration and implementation
3. Extends V/S implementation
4. Interface methods
5. Interface variables
6. Interface naming conflicts
 Method naming conflicts
 Variable naming conflicts
7. Marker interface (most valuable for interview)
8. Adapter classes
9. Interfaces v/s abstract class v/s concrete classes
10. Difference between interface and abstract class(most valuable for
interview)
11. Conclusion

Introduction:
1. Definition1: Any service requirements specification (RSS) is
considered as an interface.

Example1:
JDBC API is access requirements specification to develop database
driver.
Database vendor is responsible to implement JDBC API.

SUN-----> JDBC API------> Oracle Driver, MySQL Driver, DBL Driver

SUN people are responsible to develop JDBC API and define JDBC API.
Database vendor is responsible to implement JDBC API.

Example2:
Servlet API is access requirements to develop web server.
Web server vendor is responsible to implement Servlet API.

SUN-----> Servlet API------> Apache Tomcat server, Oracle Web logic


Server, IBM Web Sphere Server
2. Definiton2:
From client point of view, an interface define the set of
services what he is accepting.

From service provider point, an interface define the set of


services what he is offering.

Hence any contract between client and service provider is


consider as interface.

Web Application

Client Services Service provider

Interface

Example:

Client Services Service provider

Bank ATM GUI Screen

Through bank ATM GUI screen, bank people highlighting the set of service they
are offering at the same time the same GUI screen represents the set of
services what customer is accepting. Hence this GUI screen access contract
between customer and bank people.
3. Definiton3:

Inside interface every method is always public and abstract whether we are
declaring the methods and abstract or not. Hence interface is considered as
100% pure abstract class.

4. Summary Definition:

Any service requirements specification or any contract between client and


service provider or 100% pure abstract class is nothing but interface.

Interface Declaration and Implementation:


1. Whenever we are implementing an interface for each and every
method of that interface we have to provide implementation
otherwise we have to declare class as abstract class. Then next
level child class is responsible to provide implementation.

Example1: One method is implement and other is not:

package pack5;

public interface Interf {


void m1();
void m2();
}

package pack5;

abstract class ServicePerovider implements Interf {

public void m1() {


System.out.println("Withdraw Money");
}

package pack5;

public class SubServiceProvider extends ServicePerovider {

public void m2() {


System.out.println("Cash Deposit");

}
public static void main(String[] args) {
SubServiceProvider s = new SubServiceProvider();
s.m1();
s.m2();
}
}

Example2: Both methods are implement:

package pack5;

public interface Interf {


void m1();
void m2();
}

package pack5;

public class ServicePerovider implements Interf {

public void m1() {


System.out.println("Withdraw Money");
}
public void m2() {
System.out.println("Cash Deposit");
}
public static void main(String[] args) {
ServicePerovider s = new ServicePerovider();
s.m1();
s.m2();
}
}

2. Every interface method is always public and abstract whether we


are declaring the methods and abstract or not. Hence whenever we
are implementing an interface method compulsory we should declare
as public otherwise we will get compile time error.
Extends v/s Implements
1. A class can extend only one class at a time.
2. An interface can extend any number of interfaces simultaneously.

package pack4;

public interface A {

package pack4;

public interface B {

package pack4;

public interface C extends A,B {

3. A class can implement any number of interfaces simultaneously.

package pack4;

public interface A {

package pack4;

public interface B {

package pack4;
public interface C {

package pack4;

public class D implements A,B,C {

4. A class can extend another class and any number of


interfaces simultaneously.

package pack4;

public interface A {

package pack4;

public interface B {

package pack4;

public interface C {

package pack4;

public class D {

}
package pack4;

public class E extends D implements A,B,C {

}
Interface and methods
1. Every method present inside in an interface is always public and
abstract whether we are declaring or not.
2. Example:

package pack5;

public interface Interf {


void m1();

Q: Why the methods inside interface are public and abstract?

 Public: To make the method available to every implementation class.

 Abstract: Implementation class is responsible to provide


implementation.

Note:

Hence inside interface the following methods declarations are equal:

 void m1();
 public void m1();
 abstract void m1();
 public abstract void m1();

3. As every interface method is always public and abstract, we can’t


declare interface method with the following modifiers:

 Public method: private and protected

 Abstract method: static, final, synchronize , strictfp and native.


Interface variables:
1. An interface can contain variable. The main purpose of interface
variable is to define requirement level constants.

2. Every interface variable is always public, static and final


whether we are declaring or not.

package pack5;

public interface Interf {


int x = 10;
}

Q: Why the variables inside interface are public, static and final?

 Public: To make this variable available to every implementation class.

 Static: we can’t create object inside interface, so without existing


object, implementation class can access this variable.

 Final: If one implementation class changes values then remaining


implementation classes will be effected. To restrict this, every
interface variable are always final.

Note:
Hence within the interface the following declaration of variables are
equal:
 int x = 10;
 public int x = 10;
 static int x = 10;
 final int x = 10;
 public static int x = 10;
 public final int x = 10;
 static final int x = 10;
 public static final int x = 10;
3. As every interface variable is always public static final.
We can’t declare with the following modifier:

 Public variable: private and protected.


 Static variable: transient
 Final variable : volatile

4. For interface variables compulsory we should perform


initialization at the time of declaration otherwise we will get
compile time error.

package pack5;

public interface Interf {


int x;-------------------------Error

Compile time error: (=) symbol expected.

5. Inside implementation class, we can access interface variables


but we can’t modify values.

package pack5;

public interface Interf {


int x = 10;
}

package pack4;

import pack5.Interf;

public class A implements Interf {


public static void main(String[] args) {
x = 111;-----------------------------Error
System.out.println(x);
}
}

Compile time error: cannot assign a value to final variable x.

package pack5;
public interface Interf {
int x = 10;
}

package pack4;

import pack5.Interf;

public class A implements Interf {


public static void main(String[] args) {
int y = 111;
System.out.println(y);------------------111
System.out.println(x);------------------10
}
}
Interfaces naming conflicts:
 Interface Method naming conflicts:

Case1:
If two interfaces contain a method with the same signature and same
return type then in the implementation class we have to provide
implementation for only one method.

package pack5;

interface B {
void m1();
}

package pack4;

interface A {
void m1();

package pack4;

public class D implements A,B {

@Override
public void m1() {
System.out.println(1);
}

}
Case2:

If two methods contain a method with same name but different argument type
then in the implementation class we have to provide implementation for
methods and these methods as said overloaded methods.

package pack5;

public interface B {

void m1();

package pack4;

interface A {
void m1(int x);

package pack4;
import pack5.B;

public class D implements A,B {

@Override
public void m1() {
System.out.println(1);
} Overloaded methods

@Override
public void m1(int x) {
System.out.println(x);
}
public static void main(String[] args) {
D d = new D();
d.m1();
d.m1(10);
}
}
Case3:
If two interfaces contain a method with the same signature but return types
then it is impossible to implement both interfaces simultaneously (if return
types are not co-variant).

package pack4;

interface A {
void m1();

package pack5;

public interface B {

int m1();

package pack4;
import pack5.B;

public class D implements A,B {

public void m1() {---------Error


System.out.println(1);
}

public int m1() {-------------Error


return 4;
}
public static void main(String[] args) {
D d = new D();
d.m1();
d.m1(10);
}
}
Q: Is a java class can implement any number of interfaces simultaneously?

Yes, except a particular case. If two interfaces contain method with the same
signature but return type then it is important to implement both interfaces
simultaneously.
 Interface Variables naming conflicts:
1. Two interfaces can contain a variable with the same name and they
may be a chance of variable naming conflicts but we can solve
this problem by using interface names.

package pack4;

interface A {
int x = 111;

package pack5;

public interface B {

int x = 222;

package pack4;
import pack5.B;

public class D implements A,B {

public static void main(String[] args) {


System.out.println(A.x);----------------111
System.out.println(B.x);----------------222
}

}
Marker Interface/ Ability interface/ Tag
interface:
1. If an interface doesn’t contain any methods and by implementing
that interface if our objects will get some ability. Such types
of interfaces are called marker interfaces.

Example:

 Serializable interface
 Cloneable interface These are marked for some ability
 RandomAccess interface
 Single thread model interface

Example1:

Serializable interface:

By implementing serializable interface our objects can be saved to the file


and can travel across the network.

Example2:

Cloneable interface:

By implementing Clonable interface our objects are in the position to produce


exactly duplicate cloned objects.

Q: Without having any methods, how the objects get some ability in marker
interfaces?

Ans: Internally JVM is responsible to provide required ability.

Q: Why JVM is providing required ability in marker interfaces?

Ans: To reduce complexity of programming and to make java language as simple.

Q: Is it possible to create our own marker interface?

Ans: Yes but customization of JVM is required.


Adapter classes:
1. Adapter class is simple java class that implements an interface
with only empty implementation.

package pack4;

interface A {

void m1();
void m2();
void m3();
void m4();
void m5();
void m6();
void m7();
void m8();
void m9();
void m10();
.
.
.
.
.
void m1000();

package pack4;
abstract class Adapterx extends A{
void m1() { }
void m2(){ }
void m3(){ }
void m4(){ }
void m5(){ }
void m6(){ }
void m7(){ }
void m8(){ }
void m9(){ }
void m10(){ }
.
.
.
.
.
void m1000(){ };

2. If we implement an interface for each and every method of that


interface compulsory we should provide implementation whether it
is required or not required.

package pack4;

interface A {

void m1();
void m2();
void m3();
void m4();
void m5();
void m6();
void m7();
void m8();
void m9();
void m10();
.
.
.
.
.
void m1000();

package pack4;
abstract class Adapterx extends A{
void m1() {
System.out.println(1);
System.out.println(2);
System.out.println(3);
}
void m2(){ }
void m3(){ }
void m4(){ }
void m5(){ }
void m6(){ }
void m7(){ }
void m8(){ }
void m9(){ }
void m10(){ }
.
.
.
.
.
void m1000(){ };

The problem in this approach is, it increases length of the code and reduces
readability.

3. We can solve this problem by using adapter classes. Instead of


implementing interface, if we extend adapter class we have to
provide implementation only for required methods and we are not
responsible to provide implementation for each and every method
of the interface. So that length of the code will be reduces.

package pack4;

interface A {

void m1();
void m2();
void m3();
void m4();
void m5();
void m6();
void m7();
void m8();
void m9();
void m10();
.
.
.
.
.
void m1000();

package pack4;
abstract class Adapterx extends A{
void m1(){ }
void m2(){ }
void m3(){ }
void m4(){ }
void m5(){ }
void m6(){ }
void m7(){ }
void m8(){ }
void m9(){ }
void m10(){ }
.
.
.
.
.
void m1000(){ };

package pack4;

public class Test extends Adapterx {

public void m3() {

public void m7() {

}
public void m1000(){

}
}

4. We can develop a servlet in the following three ways.


 By implementing servlet interface
 By extending generic servlet(it is abstract class but
perform as adapter class for servlet interface methods )
 By extending https servlet

 Service Interface:( 5 unimplemented methods)

 Generic Servlet: Do implementation of 4 methods MyServlet Class


of Service interface except Servic methods.

 Https Servlet:

Conclusion:

 If we implement servlet interface, for each and every method of that


interface we should provide implementation. It increases length of the
code and reduces readability.
 Instead of implementing servlet interface directly, if we extend
generic servlet we have to provide implementation only for service
method and for all remaining methods we are not required to provide
implementation. Hence more or less generic servlet access adapter class
for servlet interface.

Note:

Marker interface and adapter classes simplify complexity of programming and


these are best utility to the programmer and programmer’s life will become
simple.
Interface v/s Abstract class v/s
Concrete class:
1. If we don’t anything about implementation just we have
requirement specification then we should go for interface.

Example: servlet interface.

2. If we are talking about implementation but not completely


(partial implementation then we should go for abstract class).

Example: generic servlet, https servlet etc.


3. If we are talking about implementation completely and ready to
provide service then we should go for concrete class.

Example: MyOwnServlet

Interface Servlet Interface Plan/ Design of


a Building

Abstract class Generic Servlet Partially completed


Https Servlet Building

Concrete class MyOwnServlet Fully completed


Building
Differences between Interface and Abstract Class:
Interface Abstract Class
1. If we don’t know anything about 1. If we are talking about
implementation and just we have implementation but not
requirement specification then completely (partial
we should go for interface. implementation then we should
go for abstract class.
2. Inside interface every method 2. Every method present inside
is always public and Abstract Abstract class need not be public and
whether we are declaring or abstract and we can take concrete
not. Hence interface is method also.
considered as 100% pure
abstract class.
3. As every interface method is 3. There are no restrictions on
always public and abstract and abstract class method modifiers.
hence we can’t declare with the
following modifiers:

Private, final, Protected,


static, synchronize, Native and
strictfp
4. Every variable present inside 4. Every present inside abstract
interface is always public, static class need not be public, static and
and final. Whether we are declaring final.
or not.
5. As every interface variable is 5. There are no restrictions on
always public static final. We can’t abstract class variable modifiers.
declare with the following modifiers:

Private, protected, volatile and


transient.
6. For interface variables compulsory 6. For abstract class variables, we
we should perform initialization at are not require to perform
the time of declaration only. initialization at the time of
Otherwise we will get compile time declaration.
error.
7. Inside interface we can’t declare 7. Inside abstract class we can
static and instance blocks. declare static and instance blocks.
8. Inside interface we can’t declare 8. Inside abstract class we can
constructors. declare constructor.
Q: Anyway we can’t create object for abstract class but abstract class can
contain constructor. What is the need?

Ans: Abstract class constructor will be executed whenever we are creating


child class object to perform initialization of child class object.

Example:

1. When we don’t create constructor in abstract class:

package pack6;

public abstract class Person {

String name;
int age;
long mobileNo;
String address;
String collegeName;
String marriedStatus;
int homeNo;

package pack6;

public class Student extends Person{


int marks;

Student(String name, int age, long mobileNo, String address,


String collegeName, String marriedStatus, int homeNo, int
marks ){

this.name = name;
this.age = age;
this.mobileNo = mobileNo;
this.address = address;
this.collegeName = collegeName;
this.marriedStatus = marriedStatus;
this.homeNo = homeNo;

this.marks = marks;
System.out.println(this.name);------------- Lalit
System.out.println(this.age);-------------- 26
System.out.println(this.mobileNo);--------- 999999999
System.out.println(this.address);---------- Panipat
System.out.println(this.collegeName);------ CDLU
System.out.println(this.marriedStatus);---- No
System.out.println(this.homeNo);----------- 220

System.out.println(this.marks);------------ 100
}

public static void main(String[] args) {


Student s = new Student("Lalit", 26, 999999999, "Panipat",
"CDLU", "No", 220, 100);

}
}

package pack6;

public class Teacher extends Person {


String subject;
Teacher(String name, int age, long mobileNo, String address,
String collegeName, String marriedStatus, int homeNo,
String subject){

this.name = name;
this.age = age;
this.mobileNo = mobileNo;
this.address = address;
this.collegeName = collegeName;
this.marriedStatus = marriedStatus;
this.homeNo = homeNo;

this.subject = subject;

System.out.println(this.name);----------------- Durga
System.out.println(this.age);------------------ 40
System.out.println(this.mobileNo);------------- 88888880
System.out.println(this.address);-------------- Banglore
System.out.println(this.collegeName);---------- VIT
System.out.println(this.marriedStatus);-------- Yes
System.out.println(this.homeNo);--------------- 110

System.out.println(this.subject);-------------- JAVA

public static void main(String[] args) {


Teacher t = new Teacher("Durga", 40, 88888880, "Banglore", "VIT",
"Yes", 110, "JAVA");

System.out.println(t.name);------------------- Durga
System.out.println(t.age);-------------------- 40
System.out.println(t.mobileNo);--------------- 88888880
System.out.println(t.address);---------------- Banglore
System.out.println(t.collegeName);------------ VIT
System.out.println(t.marriedStatus);---------- Yes
System.out.println(t.homeNo);----------------- 110
System.out.println(t.subject);---------------- JAVA

}
}

2. When we create constructor in abstract class:

package pack6;

public abstract class Person {


String name;
int age;
long mobileNo;
String address;
String collegeName;
String marriedStatus;
int homeNo;

Person(String name, int age, long mobileNo, String address,


String collegeName, String marriedStatus, int homeNo){

this.name = name;
this.age = age;
this.mobileNo = mobileNo;
this.address = address;
this.collegeName = collegeName;
this.marriedStatus = marriedStatus;
this.homeNo = homeNo;

package pack6;

public class Student extends Person{


int marks;

Student( int marks ){


super("Lalit", 26, 999999999, "Panipat", "CDLU", "No", 220);
this.marks = marks;

System.out.println(this.name);------------------------ Lalit
System.out.println(this.age);------------------------- 26
System.out.println(this.mobileNo);-------------------- 999999999
System.out.println(this.address);--------------------- Panipat
System.out.println(this.collegeName);----------------- CDLU
System.out.println(this.marriedStatus);--------------- No
System.out.println(this.homeNo);---------------------- 220

System.out.println(this.marks);----------------------- 100
}

public static void main(String[] args) {


Student s = new Student(100);

System.out.println(s.name);----------------------- Lalit
System.out.println(s.age);------------------------ 26
System.out.println(s.mobileNo);------------------- 999999999
System.out.println(s.address);-------------------- Panipat
System.out.println(s.collegeName);---------------- CDLU
System.out.println(s.marriedStatus);-------------- No
System.out.println(s.homeNo);--------------------- 220
System.out.println(s.marks);---------------------- 100
}
}
package pack6;

public class Teacher extends Person {


String subject;
Teacher(String subject){
super("Durga", 40, 88888880, "Banglore", "VIT", "Yes", 110);

this.subject = subject;

System.out.println(this.name);-------------------- Durga
System.out.println(this.age);--------------------- 40
System.out.println(this.mobileNo);---------------- 88888880
System.out.println(this.address);----------------- Banglore
System.out.println(this.collegeName);------------- VIT
System.out.println(this.marriedStatus);----------- Yes
System.out.println(this.homeNo);------------------ 110

System.out.println(this.subject);----------------- JAVA

public static void main(String[] args) {


Teacher t = new Teacher("JAVA");

System.out.println(t.name);------------------- Durga
System.out.println(t.age);-------------------- 40
System.out.println(t.mobileNo);--------------- 88888880
System.out.println(t.address);---------------- Banglore
System.out.println(t.collegeName);------------ VIT
System.out.println(t.marriedStatus);---------- Yes
System.out.println(t.homeNo);----------------- 110
System.out.println(t.subject);---------------- JAVA

}
}
Note:

Either directly or indirectly we can’t create object for abstract


class.

Q: Anyway we can’t objects for abstract class and interface but


abstract class can contain constructor but interface doesn’t contain.
What is the reason?

Ans:

The main purpose of constructor is to perform initialization(to provide


values) for the instance/ non-static variable.

Abstract class can contain instance variables which are required for
child object. To perform initialization of those instance variables
constructor is required for abstract class.

But every variable present inside interface is always public static


final whether we are declaring or not and there is no chance of exiting
instance variable inside interface.

Hence constructor concept is not required for interface.

3. Whenever we are creating child class object parent class object won’t
be created. Just parent class constructor will be executed for the
child class object purpose only.

Example:

package pack6;

public abstract class P {

P(){
System.out.println(this.hashCode());
}

}
package pack6;

public class C extends P {

public C() {
System.out.println(this.hashCode());
}

package pack6;

public class Test {


public static void main(String[] args) {
C c = new C();
System.out.println(c.hashCode());
}
}

Output:

366712642
366712642
366712642

Q: Inside interface every method is always abstract and we can take only
abstract methods in abstract class also then what is the difference between
interface and abstract class i.e Is it possible to replace with abstract
class?

Ans: We can replace interface with abstract class but it is not a good
programming practice. This is something like recruiting IAS Officer for
Sweeping activity.

If everything is abstract then it is highly recommended to go for interface


but not for abstract class.
Approach1: when we create abstract methods inside abstract class:

package pack6;

public abstract class P {


String name;

public abstract void m1();

P(String name){
this.name = name;
System.out.println(this.name);
System.out.println(1);
}

package pack6;

public class Test extends P {


String surName;

Test(String surName) {
super("Lalit");

this.surName = surName;

System.out.println(this.surName);
}

@Override
public void m1() {
System.out.println(2);
}

public static void main(String[] args) {

Test t = new Test("Bizlain");

System.out.println(t.name);
System.out.println(t.surName);
t.m1();
}

Approach2: when we create abstract methods inside interface:

package pack6;

interface C {

public abstract void m2();


public abstract void m3();

package pack6;

public class Test implements C {

@Override
public void m2() {
System.out.println(1);
}

@Override
public void m3() {
System.out.println(2);
}

public static void main(String[] args) {


Test t = new Test();
t.m2();
t.m3();
}

}
Approach1 Approach2

when we create abstract methods when we create abstract methods


inside abstract class: inside interface

1. We can do inherit only once class 1. We can do inherit one class and
and not other classes. So here multiple interfaces simultaneously.
inheritance problem is created. So here inheritance concept is more
useful.
2. Here child class object created 2. Here child class object created
and then parent constructor will be and direct execute interface methods
executed by child class object only. due to overriding(after implementing
So here program will take more time in child class). So here program will
to run. take less time.

3. While extending abstract class it 3. While implementing interface we


is not possible to extend any other can extend some other class and hence
class. Hence we are missing we wont
inheritance benefit. Miss inheritance benefit.
4. In this case object creation is 4. In this case object creation is
costly. not costly.

Test t = new Test( ); Test t = new Test( );

Time = 2 minutes Time = 2 seconds


Interfaces and Abstract Class
1. We cannot create object for abstract class directly, but abstract
class can contain constructor. What is need?

2. We cannot create object for abstract class directly, but


indirectly we can create it. Is it valid?

3. Whenever we are creating child class object automatically parent


class object created. Is it valid?

4. Anyway we cannot create object for abstract class and interface.


But abstract class can contain constructor but interfaces
doesn’t. why?

5. Inside interface we can create only one abstract method. But in


abstract class also we can take only abstract methods based on
our requirements, then what is the need of the interface? Is it
possible to replace interface concept with abstract class?
Roles of new keyword and constructor:
public class Student {
String name; Instance variable
int rollNo;

Student(String name,int rollNo){


this.name = name;
this.rollNo = rollNo;
}
public static void main(String[] args) {
Student s = new Student("Lalit", 100);
}
}

new keyword responsible constructor responsible to

to create object initialize the object

name = null; name = Lalit;

rollNo = 0; rollNo = 100;

Note:

1. The main objective of new keyword is to create an object.


2. The main purpose of constructor is to initialize that object.
3. First object will be created by using new keyword under then
object initialization will be performed by constructor.
Parent class

public class Person {


String name;
int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
}

Child class

public class Student extends Person {


int rollNo;
int marks;

Student(String name, int age, int rollNo, int marks){


super(name, age);
this.rollNo = rollNo;
this.marks = marks;
}

public static void main(String[] args) {


Student s = new Student("lalit",27,100,100);
}
}

name = null; name = Lalit This initialization

age = 0; age = 27; performed by parent constructor


rollNo = 0; rollNo = 100;

marks = 0; marks = 100; This initialization performed

by child constructor
package pack6;

public class Person {


String name;
int age;
Person(String name,int age){
this.name = name;
this.age = age;

System.out.println(this.name);
System.out.println(this.age);
}
}

package pack6;

public class Student extends Person {


int rollNo;
int marks;

Student(String name, int age, int rollNo, int marks){


super("Bizlain", 29);
this.rollNo = rollNo;
this.marks = marks;

System.out.println(this.rollNo);

System.out.println(this.name);
System.out.println(this.age);

System.out.println(this.marks);
}

public static void main(String[] args) {


Student s = new Student("lalit",27,100,100);
}
}
Note: When we are creating child class object Student constructor
executed and perform initialization by accessing the instance
variables of Student class using this keyword, automatically parent
constructor will be executed to perform initialization further
instance variables using super keyword which are inheriting from
parent class.

In the above program both parent and child constructor executed for
child object initialization only.

package pack6;

public class Person {


Person (){
System.out.println(this.hashCode());
}
}

package pack6;

public class Student extends Person {

Student(){
super();
System.out.println(this.hashCode());
}

package pack6;

public class Test {

public static void main(String[] args) {


Student s = new Student();
System.out.println(s.hashCode());
}

Output:

366712642
366712642
366712642

Note: When we are creating child object parent constructor executed


but parent class object won’t be created.

Parent constructor executed only for child class object.

You might also like