0% found this document useful (0 votes)
10 views35 pages

Unit Ii

The document provides an overview of class and inheritance concepts in Java, detailing types of inheritance such as single, multilevel, and hierarchical inheritance. It also covers method overriding, method overloading, abstract classes and methods, interfaces, Java packages, access modifiers, and user input handling. Key features and examples illustrate how these concepts are implemented in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views35 pages

Unit Ii

The document provides an overview of class and inheritance concepts in Java, detailing types of inheritance such as single, multilevel, and hierarchical inheritance. It also covers method overriding, method overloading, abstract classes and methods, interfaces, Java packages, access modifiers, and user input handling. Key features and examples illustrate how these concepts are implemented in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIT II

CLASS AND INHERITANCE


Inheritance
• To can be defined as the process where one class
acquires the properties (methods and fields) of
another class.

• To get the any information.

• With the use of inheritance, the information is made


manageable in a hierarchical order.
• extends is the keyword used to inherit the properties of a class.
Following is the syntax of extends keyword (define) .

class Super{
..... .....
}
class Super1 extends Super
{
..... .....
}

 There can be three types of inheritance in java:


Single inheritance
Multilevel inheritance and
Hierarchical inheritance
• Single Inheritance: refers to a child and
parent class relationship where a class
extends the another class.

• Multilevel inheritance: refers to a child and


A
parent class relationship where a class
extends the child class. For example Child 11
B
extends Child 1 and Child 1 extends parent.

C
• Hierarchical inheritance: refers to a child and
parent class relationship where more than one
classes extends the same class. For example,
Child 1 & Child 2 extends the same Parent.

• Multiple child node connected to one parent node.

• Multiple Inheritance: refers to the concept of


one class extending more than one classes, which
means a child class has two parent classes. For
example class C extends both classes A and B.
Java doesn’t support multiple inheritance,
Single Inheritance
class Teacher {

String designation = "Teacher";

String collegeName = "Beginnersbook";

void does(){

System.out.println("Teaching");

}}

public class JavaTeacher extends Teacher


OUTPUT:
{ Beginnersbook
String mainSubject = "Physics"; Teacher
Physics
public static void main(String args[]){
Teaching
JavaTeacher obj = new JavaTeacher();

System.out.println(obj.collegeName);

System.out.println(obj.designation);

System.out.println(obj.mainSubject);

obj.does();

}}
Multilevel inheritance
class Zahir{ public void distance()
int Za = 156556; {
int Ja = 80; System.out.println("Chennai to
void Zahir() Madurai Distance = " + Dis);
{ }
System.out.println("Zahir Roll No =" + Za); }
} public class ClassAdvi extends Parent{
public void mark()
{ public ClassAdvi()
System.out.println("Zahir Java Mark =" + Ja ); {
}} System.out.println("The Details of
class Parent extends Zahir{ zahir parent");
int Pr = 95956445; }
String Add = "North Street"; public static void main(String args[])
int Dis = 400; {
public Parent() ClassAdvi obj=new ClassAdvi();
{ obj.Zahir();
System.out.println("Zahir Parent Contact Details =" +obj.mark();
Pr);
} obj.address();
public void address() obj.distance();
{ }}
System.out.println("Parent Address =" + Add);
}
Hierarchical inheritance
class Car{

public Car() public void speed()

{ {

System.out.println("Class Car");
System.out.println("Max: 90Kmph");
}
}
}
public void vehicleType()
public class Maruti800 extends Maruti{
{

System.out.println("Vehicle Type: Car");


public Maruti800()
}
{
}
System.out.println("Maruti Model: 800");
class Maruti extends Car{
}
public Maruti()
public void speed()
{
{
System.out.println("Class Maruti");
System.out.println("Max: 80Kmph");
}
}
public void brand()
public static void main(String args[])
{ {
System.out.println("Brand: Maruti"); Maruti800 obj=new Maruti800();
} obj.vehicleType();
obj.brand();
obj.speed();
}}
Method overriding

• Declaring a method in sub class which is already present

in parent class is known as method overriding.

• If a subclass provides the specific implementation of the

method that has been declared by one of its parent class.


Rules for Java Method Overriding

• The method must have the same name as in the parent


class

• The method must have the same parameter as in the


parent class.

Usage

• To used to provide the specific implementation of a


method which is already provided by its superclass.
class Zahir

int Za = 156556;

void Zahir()

System.out.println("Zahir Roll No =" + Za);

}}

public class Overriding extends Zahir

void Zahir() Output


{

System.out.println("The Details of zahir"); The Details of zahir


}

public static void main(String args[])

Overriding obj=new Overriding();

obj.Zahir();

}}
Method Overloading

• Method Overloading is a feature that allows a class to


have more than one method having the same name, if
their argument lists are different.
void func() { ... }

void func(int a) { ... }

float func(double a) { ... }

float func(int a, float b) { ... }


Usage

• Method overloading is achieved by either:

changing the number of arguments.

or changing the datatype of arguments.


• Method overloading is not possible by changing the
return type of methods.
class Zahir

int za = 80;

String z = "I am Zahir";

void Zahir(int a)

System.out.println("Zahir Roll No = " + za);

void Zahir(String a)

{ Output
System.out.println("Zahir Information= " + z);

} Zahir Roll No = 80
} Zahir Information= I am Zahir
public class Test2 extends Zahir

public static void main(String args[])

Test2 obj=new Test2();

obj.Zahir(1);

obj.Zahir("");

}}
Abstract Classes and Methods
• Data abstraction is the process of hiding certain details and
showing only essential information to the user.

• Abstraction can be achieved with either abstract classes or


interfaces.

»abstract (Keyword)
• To used for classes and methods:
• Abstract class: is a restricted class that cannot be used to create

objects (to access it, it must be inherited from another class).

• Abstract method: can only be used in an abstract class, and it

does not have a body.

The abstract class or method, have to close by using ; or { }


}
abstract class BsaUniversity }
{
}
int a = 80;
class Test2
// Abstract method (does not have a body)
{
public abstract void mark();
public static void main(String[] args)
// Regular method
{
public void courses()
Zahir a = new Zahir(); // Create a Test object
{
a.courses();
System.out.println("BCA (CTIS - B)");
a.mark();
}
}
}
}
class Zahir extends BsaUniversity

public void mark() Output


{

if(a<100)
BCA (CTIS - B)
A grade
{

System.out.println("A grade");
• To create java program by using method
overriding and overloading and abstract
class and method.

• Multiple inheritance
Interfaces
• Interface in Java is a blueprint of a class.

• It has static constants and abstract methods.

• The interface in Java is a mechanism to achieve abstraction.

• It is used to achieve abstraction and multiple inheritance in


Java.

• It can have abstract methods and variables and also cannot


have a method body.
There are mainly three reasons to use interface.

• It is used to achieve abstraction.

• We can support the functionality of multiple inheritance.

• It can be used to achieve loose coupling.


interface Bsauniversity
{ System.out.println("The Java Mark for
public void manojmark(); padma");
public void padmamark(); {
} if(b<100)
class Marks implements Bsauniversity {
{ System.out.println("padma = A grade");
public void manojmark()
}}}}
{
class Test7 extends Marks
int a = 80;
{
System.out.println("The Java Mark for manoj");
{
public static void main(String[] args)
if(a<100)
{
{ Marks a = new Marks();
System.out.println("manoj = A grade"); Marks b = new Marks(); // Create a Test object
}}} a.manojmark();
public void padmamark() b.padmamark();
{ }}
int b = 85;
Java Packages
• A java package is a group of similar types of classes, interfaces
and sub-packages.

• Package in java can be categorized in two form, built-in


package and user-defined package.

• There are many built-in packages such as java, lang, awt,


javax, swing, net, io, util, sql etc.
import package.name.Class; // Import a single class

import package.name.*; // Import the whole package


Advantage of Java Package

1) Java package is used to categorize the classes and

interfaces so that they can be easily maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

https://www2.cs.duke.edu/csed/java/jdk1.5/docs/api/index.html
Scanner, To used to get user input
import java.util.Scanner;

import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}}
Access Modifiers
• java provides a number of access modifiers to set access levels
for classes, variables, methods, and constructors. The four
access levels are,
 Public Method → Accessible everywhere.
 Protected Method →Accessible within the same package
and subclasses.
 Default Method→ Accessible only within the same package.
 Private Method → Accessible only within the same class,
but accessed indirectly using accessPrivate().
2. Private (private)
• Accessible only within the same class.
• Used for encapsulation (hiding implementation
details).

class PrivateExample {

private int secretNumber = 42;

private void display()

{ System.out.println("Private method");

}}
3. Protected (protected)
• Accessible within the same package and by subclasses in
different packages.
• Used to allow subclass access while restricting outside
access.

class ProtectedExample {

protected int id = 100;

protected void show()

System.out.println("Protected method");

}}
4. Default (No Modifier)
• Also called package-private.
• Accessible only within the same package.
• If no access modifier is specified, the default is applied.

class DefaultExample {

int value = 50;

void display()

System.out.println("Default access method");

}}

You might also like