-
Abstract class:
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 body is provided by the subclass (inherited from).
Why should we use Abstract class:
Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.
- 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 body is provided by the subclass (inherited from).
Program:
abstract class Village{
String Villagename=”theni”;
int noofppl=24;
void green(){
System.out.println(“green”);
}
void agriculture(){
System.out.println(“agriculture”);
}
/*public Village(String V,int np){
V=Villagename;
np=noofppl;
System.out.println(“Village”);}*/
}subclass:
class Cultivation extends Village{
int noofseed;
String seedname;
public static void main(String args[]){
Cultivation C=new Cultivation();
C.green();
C.agriculture();
}
}output:
sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Cultivation.java
sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Cultivation
green
agriculture -
Polymorphism:
One single object which is used for many purpose that is called polymorphism.
Example : Smart watches – it is used as timing and also for calling , health porpose etc.
Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks.
Program: super class
class MoviE{
String Mname;
void playingmovie(){
System.out.println(“playingmovie”);
}
}subclass 1:
class Smovie extends MoviE{
String Sname;
void playingmovie(){
System.out.println(“playing 2D movie”);
}
}subclass 2:
class Dmovie extends MoviE{
String Dname;
void playingmovie(){
System.out.println(“playing 3D movie”);
}
}main class:
class Screen{
public static void main(String args[]){
MoviE m=new MoviE();
Smovie s=new Smovie();
Dmovie d=new Dmovie();
s.playingmovie();
d.playingmovie();
}
}Program:
sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Screen.javasobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Screenplaying 2D
movieplaying 3D movie
-
This keyword;
The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).
How we use this keyword?
This is a keyword in Java. Which can be used inside method or constructor of class. It(this) works as a reference to a current object whose method or constructor is being invoked. this keyword can be used to refer any member of current object from within an instance method or a constructor.
Program;
class Calculator{
int x=0;
int y=9;
public static void main(String args[]){
Calculator c=new Calculator();
c.x=7;
c.y=5;
c.add(2,3);
}
void add(int x,int y){
System.out.println(this.x+this.y);
System.out.println(x+y);
//this.sub(4,2);
}void sub(int x,int y)
{
System.out.println(this.x-this.y);
System.out.println(x-y);}
}output;
12
14
2
7 -
Constructor chaining :
Constructor chaining is the process of calling one constructor from another constructor with respect to current object. One of the main use of constructor chaining is to avoid duplicate codes while having multiple constructor.
reference id;
Example:(super class)
class TechPark{
final static String COURSE =”JAVA”;
/* public TechPark()
{
System.out.println(“parent def cons”);
}*/
public TechPark(int a){
System.out.println(“parent param cons”);
}
void feeStructure(){
System.out.println(19500);
}
}subclass;
class VTechPark extends TechPark{
// @Override
void feesStructure(){
System.out.println(1);
}
public VTechPark(){
super(7);
System.out.println(“child def constructor”);
}
public static void main(String[] args){
VTechPark vt=new VTechPark();
//tp.feeStructure();
}
}example:
parent param cons
child def constructor -
Homework:
Assignment 1:
class demo{
void display()
{
System.out.println(“display”);
}
public int display1(int a,int b)
{
int c=a+b;
System.out.println(c);
return c;
}
public static void main(String args[])
{
demo f=new demo();
f.display();
f.display1(-2,5);
}
}output:
display
7
Assignment 2a:
class school{
int mark;
private int salary;
static String school_name=”St.Antony’s Primary school”;
void conduct_exams(){
System.out.println(“nov 7”);
}
void publish_result(int mark)
{
System.out.println(mark);
}
}Assignment 2b:
class teacher{
public static void main(String args[])
{
school e=new school();
e.conduct_exams();
e.publish_result(100);
}
}output:
nov 7
100
Assignment 3a:
class Telephone{
int price=1500;
String name=”phone”;
public void call()
{
System.out.println(“calling facility”);
}
public void receive_call()
{
System.out.println(“receiving call facility”);
}
}Assignment 3b:
class Mobile extends Telephone{
int price=15000;
String name=”Mobile”;
public void sms()
{
System.out.println(“messaging facility”);
}
public static void main(String args[])
{
Mobile samsung=new Mobile();
samsung.call();
samsung.receive_call();
Telephone BSNL=new Telephone();
System.out.println(“BSNL.price”);
System.out.println(“BSNL.name”);
}
}output:
calling facility
receiving call facility
BSNL.price
BSNL.name -
Overriding:
Without inheritance we cant achieve overriding.In overriding we can give same datatype for two different class. In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.In overriding method and return type should be same.
Reference id:
Program:
superclass:
class woodenchair{
String color=”blue”;
void seating(){
System.out.println(“wood”);
}
public static void main(String args[]){
woodenchair w=new woodenchair();
System.out.println(w.color);
}
}subclass:
class cusion extends woodenchair{
String color="white"; //int color=2; @Override void seating(){ System.out.println("sponge");}
public static void main(String args[]){
cusion c=new cusion();
c.seating();
}
}Output:
sponge
-
Hierarchical Inheritance:
Hierarchical inheritance is one of the types of inheritance where multiple child classes inherit the methods and properties of the same parent class. Hierarchical inheritance not only reduces the code length but also increases the code modularity.

program :
super calss:
class car{
String carname;
int carnumber;
/public car(String cname, int cnum){ carname=cnum; carnumber=cnum; }/
void driving(){
System.out.println(“driving”);
}
}subclass1:
class maruticar extends car{
string mname;
void travelling(){
System.out.println(“travelling”);
}}
subclass2:
class tatacar extends car{
String modelname;
void outing(){
System.out.println(“outing”);
}
public static void main(String args[])
{
tatacar z=new tatacar();
z.driving();
z.travelling();
z.outing();
}
}output:

I have this error in output. incase of I avoid z.travelling the outing is running without any error.
for example:
class tatacar extends car{
String modelname;
void outing(){
System.out.println(“outing”);
}
public static void main(String args[])
{
tatacar z=new tatacar();
z.driving();z.outing();}
}
TBD:
By avoiding the first subclass output the program is running but other than the program shows error.
-
Inheritance:
there are five types of inheritance:
- Single-level inheritance.
- Multi-level Inheritance.
- Hierarchical Inheritance.
- Multiple Inheritance.
- Hybrid Inheritance.
single Inheritance:
Single inheritance is the simplest type of inheritance in java. In this, a class inherits the properties from a single class. The class which inherits is called the derived class or child class or subclass, while the class from which the derived class inherits is called the base class or superclass or parent class.
reference id:
The multi-level inheritance includes the involvement of at least two or more than two classes. One class inherits the features from a parent class and the newly created sub-class becomes the base class for another new class.
reference id:
Program:
class Hplaptop{
String keylang;
boolean keyLight;
boolean isTouchDisp;
/public Hplaptop(String klang, boolean klight, boolean isTouch ){ keylang = klang; keylight = klight; isTouchDisk = isTouch; }/
void watchingMovie(){
System.out.println(“watching movie”);
}
}class HpABC extends Hplaptop{
String mname;
void coding(){
System.out.println(“coding”);
}
}class Hpcse extends HpABC{
String modelname;
void playingGame(){
System.out.println(“playing Game”);
}
public static void main(String args[])
{
Hpcse c=new Hpcse();
c.coding();
c.watchingMovie();
c.playingGame();
}
}output:
coding
watchingMovie
playingGame
-
Inheritance:
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Reference id:
https://www.javatpoint.com/inheritance-in-java
Why inheritance is used in java:
for method overriding.
for code reuseble.
The most important use of inheritance in Java is code reusability. The code that is present in the parent class can be directly used by the child class.The key to understanding Inheritance is that it provides code re-usability. In place of writing the same code, again and again, we can simply inherit the properties of one class into the other.
When inheritance is used:
- Both classes are in the same logical domain.
- The subclass is a proper subtype of the superclass.
- The superclass’s implementation is necessary or appropriate for the subclass.
- The enhancements made by the subclass are primarily additive.
Reference id
https://www.google.com/search?client=firefox-b-lm&q=when+inheritance+is+usedv
-
Contructor:
Java constructors or constructors in Java is a terminology been used to construct something in our programs. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object.
example 1:
class PE{
String tshirt;
int size;
public PE(String shirt){
tshirt=shirt;System.out.println(“shirt”+shirt);
}
public PE(String shirt,int S)
{
tshirt=tshirt;
size=size;
System.out.println(“shirt”+shirt+”\n”+S);
}
public static void main(String args[])
{
PE o=new PE(“abc”);
PE k=new PE(“abs”,24);
System.out.println(k.tshirt);
System.out.println(k.size);
}}
output
shirtabc
shirtabs
24
null
0
example2:
class collage{
String name;
int stuid;
boolean pass;
public collage(String N, int ID)
{String name=”N”;
stuid=ID;
System.out.println(“name “+N+”\n”+ID);
}public collage(String N, int ID,boolean T)
{
String name=”N”;
stuid=ID;
pass=T;System.out.println(“name “+N+”\n”+ID+”\n”+T);
}
public static void main(String args[])
{
collage s=new collage(“sobhika”,123);
collage p=new collage(“ramya”,456,true);
}
}output;
name sobhika
123
name ramya
456
true
refrence id: