* Mybook.java
Public class Mybook {
int x=10;
int y=8;
}
* count.java
Class Count {
Public static void main(String[] args)
{
Mybook myobj1 = new myobj1();
Mybook myobj2 = new myobj2();
System.out.println(myobj1.x);
System.out.println(myobj2,y);
}
}
Public class Mybook {
int x=10;
int y=8;
}
* count.java
Class Count {
Public static void main(String[] args)
{
Mybook myobj1 = new myobj1();
Mybook myobj2 = new myobj2();
System.out.println(myobj1.x);
System.out.println(myobj2,y);
}
}
What are Classes ?
Classes are like object constructors for creating objects. The collection of objects is said to be a class. Classes are said to be logical quantities. Classes don’t consume any space in the memory. Class is also called a template of an object. Classes have members which can be fields, methods and constructors. A class has both static and instance initializers.
A class declaration consists of:
1. Modifiers: These can be public or default access.
2. Class name: Initial letter.
3. Superclass: A class can only extend (subclass) one parent.
4. Interfaces: A class can implement more than one interface.
5. Body: Body surrounded by braces, { }.
Classes are like object constructors for creating objects. The collection of objects is said to be a class. Classes are said to be logical quantities. Classes don’t consume any space in the memory. Class is also called a template of an object. Classes have members which can be fields, methods and constructors. A class has both static and instance initializers.
A class declaration consists of:
1. Modifiers: These can be public or default access.
2. Class name: Initial letter.
3. Superclass: A class can only extend (subclass) one parent.
4. Interfaces: A class can implement more than one interface.
5. Body: Body surrounded by braces, { }.
A class keyword is used to create a class. A simplified general form of the class definition is given below:
class classname {
type instance variable 1;
type instance variable 2;
.
.
.
type instance variable n;
type methodname 1 (parameter list) {
// body od method
}
type methodname 2 (parameter list) {
// body od method
}
type methodnamen(parameter list) {
// body od method
}
}
class classname {
type instance variable 1;
type instance variable 2;
.
.
.
type instance variable n;
type methodname 1 (parameter list) {
// body od method
}
type methodname 2 (parameter list) {
// body od method
}
type methodnamen(parameter list) {
// body od method
}
}
The variables or data defined within a class are called instance variables. Code is always contained in the methods. Therefore, the methods and variables defined within a class are called members of the class. All the methods have the same form as the main () these methods are not specified as static or public.
What is Abstract ?
Abstraction is a process which displays only the information needed and hides the unnecessary information. We can say that the main purpose of abstraction is data hiding. Abstraction means selecting data from a large number of data to show the information needed, which helps in reducing programming complexity and efforts.
Abstraction is a process which displays only the information needed and hides the unnecessary information. We can say that the main purpose of abstraction is data hiding. Abstraction means selecting data from a large number of data to show the information needed, which helps in reducing programming complexity and efforts.
There are also abstract classes and abstract methods. An abstract class is a type of class that declares one or more abstract methods. An abstract method is a method that has a method definition but not implementation. Once we have modelled our object using data abstraction, the same sets of data can also be used in different applications—abstract classes, generic types of behaviours and object-oriented programming hierarchy. Abstract methods are used when two or more subclasses do the same task in different ways and through different implementations. An abstract class can have both methods, i.e., abstract methods and regular methods.
We may not require every information that we have collected to fill out the application. So, we select the data that is required to fill out the application. Hence, we have fetched, removed, and selected the data, the student information from large data. This process is known as abstraction in the oops concept.
What is Inheritance ?
Inheritance is a method in which one object acquires/inherits another object’s properties, and inheritance also supports hierarchical classification. The idea behind this is that we can create new classes built on existing classes, i.e., when you inherit from an existing class, we can reuse methods and fields of the parent class. Inheritance represents the parent-child relationship.
Inheritance is a method in which one object acquires/inherits another object’s properties, and inheritance also supports hierarchical classification. The idea behind this is that we can create new classes built on existing classes, i.e., when you inherit from an existing class, we can reuse methods and fields of the parent class. Inheritance represents the parent-child relationship.
For example, a whale is a part of the classification of marine animals, which is part of class mammal, which is under that class of animal. We use hierarchical classification, i.e., top-down classification. If we want to describe a more specific class of animals such as mammals, they would have more specific attributes such as teeth; cold-blooded, warm-blooded, etc. This comes under the subclass of animals whereas animals come under the superclass. The subclass is a class which inherits properties of the superclass. This is also called a derived class. A superclass is a base class or parental class from which a subclass inherits properties.
We use inheritance mainly for method overriding and R:
To inherit a class, we use the extend keyword.
There are five types of inheritance single, multilevel, multiple, hybrid and hierarchical.
We use inheritance mainly for method overriding and R:
To inherit a class, we use the extend keyword.
There are five types of inheritance single, multilevel, multiple, hybrid and hierarchical.
* Hybrid inheritance
This is the combination of multiple and multilevel inheritances and in java, multiple inheritances are not supported as it leads to ambiguity and this type of inheritance can only be achieved through interfaces.
Consider that class a is the parental or base class of class b and class c and in turn, class b and class c are parental or a base class of class d. Class b and class c are derived classes from class a and class d is derived class from class b and class c.
The following program creates a superclass called add and a subclass called sub, using extend keyword to create a subclass add.
This is the combination of multiple and multilevel inheritances and in java, multiple inheritances are not supported as it leads to ambiguity and this type of inheritance can only be achieved through interfaces.
Consider that class a is the parental or base class of class b and class c and in turn, class b and class c are parental or a base class of class d. Class b and class c are derived classes from class a and class d is derived class from class b and class c.
The following program creates a superclass called add and a subclass called sub, using extend keyword to create a subclass add.
// a simple example of inheritance
// create a superclass
class Add {
int my;
int by;
void setmyby (int xy, int hy) {
my=xy;
by=hy;
}
}
// create a sub class
class b extends add {
int total;
void sum() {
public Static void main(String args[]) {
b sub0b = new b();
sub0b.Setmyby(10,12);
sub0b.Sum();
System.out.println("total = " + sub0b.Total);
}
}
// create a superclass
class Add {
int my;
int by;
void setmyby (int xy, int hy) {
my=xy;
by=hy;
}
}
// create a sub class
class b extends add {
int total;
void sum() {
public Static void main(String args[]) {
b sub0b = new b();
sub0b.Setmyby(10,12);
sub0b.Sum();
System.out.println("total = " + sub0b.Total);
}
}
What is Polymorphism ?
Polymorphism refers to many forms, or it is a process that performs a single action in different ways. It occurs when we have many classes related to each other by inheritance. Polymorphism is of two different types, i.e., compile-time polymorphism and runtime polymorphism. One of the examples of Compile time polymorphism is that when we overload a static method in java. Run time polymorphism also called a dynamic method dispatch is a method in which a call to an overridden method is resolved at run time rather than compile time. In this method, the overridden method is always called through the reference variable. By using method overloading and method overriding, we can perform polymorphism. Generally, the concept of polymorphism is often expressed as one interface, and multiple methods. This reduces complexity by allowing the same interface to be used as a general class of action.
Polymorphism refers to many forms, or it is a process that performs a single action in different ways. It occurs when we have many classes related to each other by inheritance. Polymorphism is of two different types, i.e., compile-time polymorphism and runtime polymorphism. One of the examples of Compile time polymorphism is that when we overload a static method in java. Run time polymorphism also called a dynamic method dispatch is a method in which a call to an overridden method is resolved at run time rather than compile time. In this method, the overridden method is always called through the reference variable. By using method overloading and method overriding, we can perform polymorphism. Generally, the concept of polymorphism is often expressed as one interface, and multiple methods. This reduces complexity by allowing the same interface to be used as a general class of action.